blob: 09667a1adfb6cd4d37d9f1cf8b15524cd7bda9db [file] [log] [blame]
Todd Poynor752faf22013-06-12 13:25:59 -07001/*
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>
Todd Poynor020369d2013-09-18 20:09:33 -070021#include <binder/IPCThreadState.h>
Todd Poynor752faf22013-06-12 13:25:59 -070022#include <binder/IServiceManager.h>
Todd Poynor020369d2013-09-18 20:09:33 -070023#include <binder/PermissionCache.h>
24#include <private/android_filesystem_config.h>
Todd Poynor752faf22013-06-12 13:25:59 -070025#include <utils/Errors.h>
26#include <utils/Mutex.h>
27#include <utils/String16.h>
28
Todd Poynor7b27f272013-09-06 15:14:24 -070029#include "healthd.h"
Todd Poynor752faf22013-06-12 13:25:59 -070030
Todd Poynor7b27f272013-09-06 15:14:24 -070031namespace android {
Todd Poynor752faf22013-06-12 13:25:59 -070032
33void BatteryPropertiesRegistrar::publish() {
Todd Poynorf0e2ac22013-10-22 17:50:59 -070034 defaultServiceManager()->addService(String16("batteryproperties"), this);
Todd Poynor752faf22013-06-12 13:25:59 -070035}
36
37void BatteryPropertiesRegistrar::notifyListeners(struct BatteryProperties props) {
38 Mutex::Autolock _l(mRegistrationLock);
39 for (size_t i = 0; i < mListeners.size(); i++) {
40 mListeners[i]->batteryPropertiesChanged(props);
41 }
42}
43
44void BatteryPropertiesRegistrar::registerListener(const sp<IBatteryPropertiesListener>& listener) {
45 {
Natalie Silvanovich3e6d3312014-04-30 14:50:27 -070046 if (listener == NULL)
47 return;
Todd Poynor752faf22013-06-12 13:25:59 -070048 Mutex::Autolock _l(mRegistrationLock);
49 // check whether this is a duplicate
50 for (size_t i = 0; i < mListeners.size(); i++) {
Marco Nelissen99f00d52014-11-14 08:03:02 -080051 if (IInterface::asBinder(mListeners[i]) == IInterface::asBinder(listener)) {
Todd Poynor752faf22013-06-12 13:25:59 -070052 return;
53 }
54 }
55
56 mListeners.add(listener);
Marco Nelissen99f00d52014-11-14 08:03:02 -080057 IInterface::asBinder(listener)->linkToDeath(this);
Todd Poynor752faf22013-06-12 13:25:59 -070058 }
Todd Poynor7b27f272013-09-06 15:14:24 -070059 healthd_battery_update();
Todd Poynor752faf22013-06-12 13:25:59 -070060}
61
62void BatteryPropertiesRegistrar::unregisterListener(const sp<IBatteryPropertiesListener>& listener) {
Natalie Silvanovich3e6d3312014-04-30 14:50:27 -070063 if (listener == NULL)
64 return;
Todd Poynor752faf22013-06-12 13:25:59 -070065 Mutex::Autolock _l(mRegistrationLock);
66 for (size_t i = 0; i < mListeners.size(); i++) {
Marco Nelissen99f00d52014-11-14 08:03:02 -080067 if (IInterface::asBinder(mListeners[i]) == IInterface::asBinder(listener)) {
68 IInterface::asBinder(mListeners[i])->unlinkToDeath(this);
Todd Poynor752faf22013-06-12 13:25:59 -070069 mListeners.removeAt(i);
70 break;
71 }
72 }
73}
74
Todd Poynorc133b712013-08-14 17:39:13 -070075status_t BatteryPropertiesRegistrar::getProperty(int id, struct BatteryProperty *val) {
Todd Poynor7b27f272013-09-06 15:14:24 -070076 return healthd_get_property(id, val);
Todd Poynorc133b712013-08-14 17:39:13 -070077}
78
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -070079status_t BatteryPropertiesRegistrar::dump(int fd, const Vector<String16>& /*args*/) {
Todd Poynor020369d2013-09-18 20:09:33 -070080 IPCThreadState* self = IPCThreadState::self();
81 const int pid = self->getCallingPid();
82 const int uid = self->getCallingUid();
83 if ((uid != AID_SHELL) &&
84 !PermissionCache::checkPermission(
85 String16("android.permission.DUMP"), pid, uid))
86 return PERMISSION_DENIED;
87
88 healthd_dump_battery_state(fd);
89 return OK;
90}
91
Todd Poynor752faf22013-06-12 13:25:59 -070092void BatteryPropertiesRegistrar::binderDied(const wp<IBinder>& who) {
93 Mutex::Autolock _l(mRegistrationLock);
94
95 for (size_t i = 0; i < mListeners.size(); i++) {
Marco Nelissen99f00d52014-11-14 08:03:02 -080096 if (IInterface::asBinder(mListeners[i]) == who) {
Todd Poynor752faf22013-06-12 13:25:59 -070097 mListeners.removeAt(i);
98 break;
99 }
100 }
101}
102
103} // namespace android