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(struct BatteryProperties props)38 void BatteryPropertiesRegistrar::notifyListeners(struct BatteryProperties props) {
39 Mutex::Autolock _l(mRegistrationLock);
40 for (size_t i = 0; i < mListeners.size(); i++) {
41 mListeners[i]->batteryPropertiesChanged(props);
42 }
43 }
44
registerListener(const sp<IBatteryPropertiesListener> & listener)45 void BatteryPropertiesRegistrar::registerListener(const sp<IBatteryPropertiesListener>& listener) {
46 {
47 if (listener == NULL)
48 return;
49 Mutex::Autolock _l(mRegistrationLock);
50 // check whether this is a duplicate
51 for (size_t i = 0; i < mListeners.size(); i++) {
52 if (IInterface::asBinder(mListeners[i]) == IInterface::asBinder(listener)) {
53 return;
54 }
55 }
56
57 mListeners.add(listener);
58 IInterface::asBinder(listener)->linkToDeath(this);
59 }
60 healthd_battery_update();
61 }
62
unregisterListener(const sp<IBatteryPropertiesListener> & listener)63 void BatteryPropertiesRegistrar::unregisterListener(const sp<IBatteryPropertiesListener>& listener) {
64 if (listener == NULL)
65 return;
66 Mutex::Autolock _l(mRegistrationLock);
67 for (size_t i = 0; i < mListeners.size(); i++) {
68 if (IInterface::asBinder(mListeners[i]) == IInterface::asBinder(listener)) {
69 IInterface::asBinder(mListeners[i])->unlinkToDeath(this);
70 mListeners.removeAt(i);
71 break;
72 }
73 }
74 }
75
getProperty(int id,struct BatteryProperty * val)76 status_t BatteryPropertiesRegistrar::getProperty(int id, struct BatteryProperty *val) {
77 return healthd_get_property(id, val);
78 }
79
dump(int fd,const Vector<String16> &)80 status_t BatteryPropertiesRegistrar::dump(int fd, const Vector<String16>& /*args*/) {
81 IPCThreadState* self = IPCThreadState::self();
82 const int pid = self->getCallingPid();
83 const int uid = self->getCallingUid();
84 if ((uid != AID_SHELL) &&
85 !PermissionCache::checkPermission(
86 String16("android.permission.DUMP"), pid, uid))
87 return PERMISSION_DENIED;
88
89 healthd_dump_battery_state(fd);
90 return OK;
91 }
92
binderDied(const wp<IBinder> & who)93 void BatteryPropertiesRegistrar::binderDied(const wp<IBinder>& who) {
94 Mutex::Autolock _l(mRegistrationLock);
95
96 for (size_t i = 0; i < mListeners.size(); i++) {
97 if (IInterface::asBinder(mListeners[i]) == who) {
98 mListeners.removeAt(i);
99 break;
100 }
101 }
102 }
103
104 } // namespace android
105