• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
18 #include <android/os/IThermalService.h>
19 #include <android/os/IThermalEventListener.h>
20 #include <android/os/Temperature.h>
21 #include <binder/IPCThreadState.h>
22 #include <binder/IServiceManager.h>
23 #include <utils/Errors.h>
24 #include <utils/Mutex.h>
25 #include <utils/String16.h>
26 
27 namespace android {
28 namespace os {
29 
30 /**
31  * Notify registered listeners of a thermal throttling start/stop event.
32  * @param temperature the temperature at which the event was generated
33  */
notifyThrottling(const bool isThrottling,const Temperature & temperature)34 binder::Status ThermalService::notifyThrottling(
35     const bool isThrottling, const Temperature& temperature) {
36     Mutex::Autolock _l(mListenersLock);
37 
38     mThrottled = isThrottling;
39     mThrottleTemperature = temperature;
40 
41     for (size_t i = 0; i < mListeners.size(); i++) {
42       mListeners[i]->notifyThrottling(isThrottling, temperature);
43     }
44     return binder::Status::ok();
45 }
46 
47 /**
48  * Query whether the system is currently thermal throttling.
49  * @return true if currently thermal throttling, else false
50  */
isThrottling(bool * _aidl_return)51 binder::Status ThermalService::isThrottling(bool* _aidl_return) {
52     Mutex::Autolock _l(mListenersLock);
53     *_aidl_return = mThrottled;
54     return binder::Status::ok();
55 }
56 
57 /**
58  * Register a new thermal event listener.
59  * @param listener the client's IThermalEventListener instance to which
60  *                 notifications are to be sent
61  */
registerThermalEventListener(const sp<IThermalEventListener> & listener)62 binder::Status ThermalService::registerThermalEventListener(
63     const sp<IThermalEventListener>& listener) {
64     {
65         if (listener == NULL)
66             return binder::Status::ok();
67         Mutex::Autolock _l(mListenersLock);
68         // check whether this is a duplicate
69         for (size_t i = 0; i < mListeners.size(); i++) {
70             if (IInterface::asBinder(mListeners[i]) ==
71                 IInterface::asBinder(listener)) {
72                 return binder::Status::ok();
73             }
74         }
75 
76         mListeners.add(listener);
77         IInterface::asBinder(listener)->linkToDeath(this);
78     }
79 
80     return binder::Status::ok();
81 }
82 
83 /**
84  * Unregister a previously-registered thermal event listener.
85  * @param listener the client's IThermalEventListener instance to which
86  *                 notifications are to no longer be sent
87  */
unregisterThermalEventListener(const sp<IThermalEventListener> & listener)88 binder::Status ThermalService::unregisterThermalEventListener(
89     const sp<IThermalEventListener>& listener) {
90     if (listener == NULL)
91         return binder::Status::ok();
92     Mutex::Autolock _l(mListenersLock);
93     for (size_t i = 0; i < mListeners.size(); i++) {
94         if (IInterface::asBinder(mListeners[i]) ==
95             IInterface::asBinder(listener)) {
96             IInterface::asBinder(mListeners[i])->unlinkToDeath(this);
97             mListeners.removeAt(i);
98             break;
99         }
100     }
101 
102     return binder::Status::ok();
103 }
104 
binderDied(const wp<IBinder> & who)105 void ThermalService::binderDied(const wp<IBinder>& who) {
106     Mutex::Autolock _l(mListenersLock);
107 
108     for (size_t i = 0; i < mListeners.size(); i++) {
109         if (IInterface::asBinder(mListeners[i]) == who) {
110             mListeners.removeAt(i);
111             break;
112         }
113     }
114 }
115 
116 /**
117  * Publish the supplied ThermalService to servicemanager.
118  */
publish(const sp<ThermalService> & service)119 void ThermalService::publish(
120     const sp<ThermalService>& service) {
121     defaultServiceManager()->addService(String16("thermalservice"),
122                                         service);
123 }
124 
125 }  // namespace os
126 }  // namespace android
127