Todd Poynor | 539d256 | 2017-05-24 17:19:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #include "ThermalService.h" |
Wei Wang | 5215000 | 2018-10-24 19:33:10 -0700 | [diff] [blame] | 18 | #include <android-base/file.h> |
| 19 | #include <android-base/stringprintf.h> |
Todd Poynor | 539d256 | 2017-05-24 17:19:18 -0700 | [diff] [blame] | 20 | #include <android/os/IThermalEventListener.h> |
Wei Wang | 5215000 | 2018-10-24 19:33:10 -0700 | [diff] [blame] | 21 | #include <android/os/IThermalService.h> |
Todd Poynor | 539d256 | 2017-05-24 17:19:18 -0700 | [diff] [blame] | 22 | #include <android/os/Temperature.h> |
| 23 | #include <binder/IPCThreadState.h> |
| 24 | #include <binder/IServiceManager.h> |
Wei Wang | 5215000 | 2018-10-24 19:33:10 -0700 | [diff] [blame] | 25 | #include <binder/PermissionCache.h> |
| 26 | #include <log/log.h> |
| 27 | #include <private/android_filesystem_config.h> |
Todd Poynor | 539d256 | 2017-05-24 17:19:18 -0700 | [diff] [blame] | 28 | #include <utils/Errors.h> |
| 29 | #include <utils/Mutex.h> |
| 30 | #include <utils/String16.h> |
| 31 | |
| 32 | namespace android { |
| 33 | namespace os { |
| 34 | |
| 35 | /** |
Wei Wang | 5215000 | 2018-10-24 19:33:10 -0700 | [diff] [blame] | 36 | * Dump thermal service |
| 37 | * @param fd file descriptor for dumping |
| 38 | * @param args not used |
| 39 | */ |
| 40 | status_t ThermalService::dump(int fd, const Vector<String16>& /* args */) { |
| 41 | status_t ret = OK; |
| 42 | std::string result; |
| 43 | const IPCThreadState* ipc = IPCThreadState::self(); |
| 44 | const int pid = ipc->getCallingPid(); |
| 45 | const int uid = ipc->getCallingUid(); |
| 46 | if ((uid != AID_SHELL) && |
| 47 | !PermissionCache::checkPermission(String16("android.permission.DUMP"), pid, uid)) { |
| 48 | result = android::base:: |
| 49 | StringPrintf("Permission Denial: can't dump ThermalService from pid=%d, uid=%d\n", |
| 50 | pid, uid); |
| 51 | ret = PERMISSION_DENIED; |
| 52 | } else { |
| 53 | Mutex::Autolock _l(mListenersLock); |
| 54 | result = android::base::StringPrintf("ThermalEventListener registered: %d\n", |
| 55 | (int)mListeners.size()); |
| 56 | } |
| 57 | if (!android::base::WriteStringToFd(result, fd)) { |
| 58 | SLOGE("Failed to dump fd: %d", fd); |
| 59 | ret = FDS_NOT_ALLOWED; |
| 60 | } |
| 61 | return ret; |
| 62 | } |
| 63 | |
| 64 | /** |
Todd Poynor | 539d256 | 2017-05-24 17:19:18 -0700 | [diff] [blame] | 65 | * Notify registered listeners of a thermal throttling start/stop event. |
| 66 | * @param temperature the temperature at which the event was generated |
| 67 | */ |
| 68 | binder::Status ThermalService::notifyThrottling( |
| 69 | const bool isThrottling, const Temperature& temperature) { |
| 70 | Mutex::Autolock _l(mListenersLock); |
| 71 | |
| 72 | mThrottled = isThrottling; |
| 73 | mThrottleTemperature = temperature; |
| 74 | |
| 75 | for (size_t i = 0; i < mListeners.size(); i++) { |
| 76 | mListeners[i]->notifyThrottling(isThrottling, temperature); |
| 77 | } |
| 78 | return binder::Status::ok(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Query whether the system is currently thermal throttling. |
| 83 | * @return true if currently thermal throttling, else false |
| 84 | */ |
| 85 | binder::Status ThermalService::isThrottling(bool* _aidl_return) { |
| 86 | Mutex::Autolock _l(mListenersLock); |
| 87 | *_aidl_return = mThrottled; |
| 88 | return binder::Status::ok(); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Register a new thermal event listener. |
| 93 | * @param listener the client's IThermalEventListener instance to which |
| 94 | * notifications are to be sent |
| 95 | */ |
| 96 | binder::Status ThermalService::registerThermalEventListener( |
| 97 | const sp<IThermalEventListener>& listener) { |
| 98 | { |
Wei Wang | 5215000 | 2018-10-24 19:33:10 -0700 | [diff] [blame] | 99 | if (listener == NULL) { |
Todd Poynor | 539d256 | 2017-05-24 17:19:18 -0700 | [diff] [blame] | 100 | return binder::Status::ok(); |
Wei Wang | 5215000 | 2018-10-24 19:33:10 -0700 | [diff] [blame] | 101 | } |
Todd Poynor | 539d256 | 2017-05-24 17:19:18 -0700 | [diff] [blame] | 102 | Mutex::Autolock _l(mListenersLock); |
| 103 | // check whether this is a duplicate |
| 104 | for (size_t i = 0; i < mListeners.size(); i++) { |
| 105 | if (IInterface::asBinder(mListeners[i]) == |
| 106 | IInterface::asBinder(listener)) { |
| 107 | return binder::Status::ok(); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | mListeners.add(listener); |
| 112 | IInterface::asBinder(listener)->linkToDeath(this); |
| 113 | } |
| 114 | |
| 115 | return binder::Status::ok(); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Unregister a previously-registered thermal event listener. |
| 120 | * @param listener the client's IThermalEventListener instance to which |
| 121 | * notifications are to no longer be sent |
| 122 | */ |
| 123 | binder::Status ThermalService::unregisterThermalEventListener( |
| 124 | const sp<IThermalEventListener>& listener) { |
Wei Wang | 5215000 | 2018-10-24 19:33:10 -0700 | [diff] [blame] | 125 | if (listener == NULL) { |
Todd Poynor | 539d256 | 2017-05-24 17:19:18 -0700 | [diff] [blame] | 126 | return binder::Status::ok(); |
Wei Wang | 5215000 | 2018-10-24 19:33:10 -0700 | [diff] [blame] | 127 | } |
Todd Poynor | 539d256 | 2017-05-24 17:19:18 -0700 | [diff] [blame] | 128 | Mutex::Autolock _l(mListenersLock); |
| 129 | for (size_t i = 0; i < mListeners.size(); i++) { |
| 130 | if (IInterface::asBinder(mListeners[i]) == |
| 131 | IInterface::asBinder(listener)) { |
| 132 | IInterface::asBinder(mListeners[i])->unlinkToDeath(this); |
| 133 | mListeners.removeAt(i); |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return binder::Status::ok(); |
| 139 | } |
| 140 | |
| 141 | void ThermalService::binderDied(const wp<IBinder>& who) { |
| 142 | Mutex::Autolock _l(mListenersLock); |
| 143 | |
| 144 | for (size_t i = 0; i < mListeners.size(); i++) { |
| 145 | if (IInterface::asBinder(mListeners[i]) == who) { |
| 146 | mListeners.removeAt(i); |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Publish the supplied ThermalService to servicemanager. |
| 154 | */ |
| 155 | void ThermalService::publish( |
| 156 | const sp<ThermalService>& service) { |
| 157 | defaultServiceManager()->addService(String16("thermalservice"), |
| 158 | service); |
| 159 | } |
| 160 | |
| 161 | } // namespace os |
| 162 | } // namespace android |