• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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 "BatteryPropertiesRegistrar.h"
18 #include <batteryservice/BatteryService.h>
19 #include <batteryservice/IBatteryPropertiesListener.h>
20 #include <batteryservice/IBatteryPropertiesRegistrar.h>
21 #include <binder/IPCThreadState.h>
22 #include <binder/IServiceManager.h>
23 #include <binder/PermissionCache.h>
24 #include <private/android_filesystem_config.h>
25 #include <utils/Errors.h>
26 #include <utils/Mutex.h>
27 #include <utils/String16.h>
28 
29 #include <healthd/healthd.h>
30 
31 namespace android {
32 
publish(const sp<BatteryPropertiesRegistrar> & service)33 void BatteryPropertiesRegistrar::publish(
34     const sp<BatteryPropertiesRegistrar>& service) {
35     defaultServiceManager()->addService(String16("batteryproperties"), service);
36 }
37 
notifyListeners(const struct BatteryProperties & props)38 void BatteryPropertiesRegistrar::notifyListeners(const struct BatteryProperties& props) {
39     Vector<sp<IBatteryPropertiesListener> > listenersCopy;
40 
41     // Binder currently may service an incoming oneway transaction whenever an
42     // outbound oneway call is made (if there is already a pending incoming
43     // oneway call waiting).  This is considered a bug and may change in the
44     // future.  For now, avoid recursive mutex lock while making outbound
45     // calls by making a local copy of the current list of listeners.
46     {
47         Mutex::Autolock _l(mRegistrationLock);
48         listenersCopy = mListeners;
49     }
50     for (size_t i = 0; i < listenersCopy.size(); i++) {
51         listenersCopy[i]->batteryPropertiesChanged(props);
52     }
53 }
54 
registerListener(const sp<IBatteryPropertiesListener> & listener)55 void BatteryPropertiesRegistrar::registerListener(const sp<IBatteryPropertiesListener>& listener) {
56     {
57         if (listener == NULL)
58             return;
59         Mutex::Autolock _l(mRegistrationLock);
60         // check whether this is a duplicate
61         for (size_t i = 0; i < mListeners.size(); i++) {
62             if (IInterface::asBinder(mListeners[i]) == IInterface::asBinder(listener)) {
63                 return;
64             }
65         }
66 
67         mListeners.add(listener);
68         IInterface::asBinder(listener)->linkToDeath(this);
69     }
70     healthd_battery_update();
71 }
72 
unregisterListener(const sp<IBatteryPropertiesListener> & listener)73 void BatteryPropertiesRegistrar::unregisterListener(const sp<IBatteryPropertiesListener>& listener) {
74     if (listener == NULL)
75         return;
76     Mutex::Autolock _l(mRegistrationLock);
77     for (size_t i = 0; i < mListeners.size(); i++) {
78         if (IInterface::asBinder(mListeners[i]) == IInterface::asBinder(listener)) {
79             IInterface::asBinder(mListeners[i])->unlinkToDeath(this);
80             mListeners.removeAt(i);
81             break;
82         }
83     }
84 }
85 
getProperty(int id,struct BatteryProperty * val)86 status_t BatteryPropertiesRegistrar::getProperty(int id, struct BatteryProperty *val) {
87     return healthd_get_property(id, val);
88 }
89 
scheduleUpdate()90 void BatteryPropertiesRegistrar::scheduleUpdate() {
91     healthd_battery_update();
92 }
93 
dump(int fd,const Vector<String16> &)94 status_t BatteryPropertiesRegistrar::dump(int fd, const Vector<String16>& /*args*/) {
95     IPCThreadState* self = IPCThreadState::self();
96     const int pid = self->getCallingPid();
97     const int uid = self->getCallingUid();
98     if ((uid != AID_SHELL) &&
99         !PermissionCache::checkPermission(
100                 String16("android.permission.DUMP"), pid, uid))
101         return PERMISSION_DENIED;
102 
103     healthd_dump_battery_state(fd);
104     return OK;
105 }
106 
binderDied(const wp<IBinder> & who)107 void BatteryPropertiesRegistrar::binderDied(const wp<IBinder>& who) {
108     Mutex::Autolock _l(mRegistrationLock);
109 
110     for (size_t i = 0; i < mListeners.size(); i++) {
111         if (IInterface::asBinder(mListeners[i]) == who) {
112             mListeners.removeAt(i);
113             break;
114         }
115     }
116 }
117 
118 }  // namespace android
119