blob: 52e6be06eceb2ada1ffa11ea9dda2b33e70c15de [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() {
34 defaultServiceManager()->addService(String16("batterypropreg"), this);
Todd Poynorf0e2ac22013-10-22 17:50:59 -070035 defaultServiceManager()->addService(String16("batteryproperties"), this);
Todd Poynor752faf22013-06-12 13:25:59 -070036}
37
38void 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
45void BatteryPropertiesRegistrar::registerListener(const sp<IBatteryPropertiesListener>& listener) {
46 {
47 Mutex::Autolock _l(mRegistrationLock);
48 // check whether this is a duplicate
49 for (size_t i = 0; i < mListeners.size(); i++) {
50 if (mListeners[i]->asBinder() == listener->asBinder()) {
51 return;
52 }
53 }
54
55 mListeners.add(listener);
56 listener->asBinder()->linkToDeath(this);
57 }
Todd Poynor7b27f272013-09-06 15:14:24 -070058 healthd_battery_update();
Todd Poynor752faf22013-06-12 13:25:59 -070059}
60
61void BatteryPropertiesRegistrar::unregisterListener(const sp<IBatteryPropertiesListener>& listener) {
62 Mutex::Autolock _l(mRegistrationLock);
63 for (size_t i = 0; i < mListeners.size(); i++) {
64 if (mListeners[i]->asBinder() == listener->asBinder()) {
65 mListeners[i]->asBinder()->unlinkToDeath(this);
66 mListeners.removeAt(i);
67 break;
68 }
69 }
70}
71
Todd Poynorc133b712013-08-14 17:39:13 -070072status_t BatteryPropertiesRegistrar::getProperty(int id, struct BatteryProperty *val) {
Todd Poynor7b27f272013-09-06 15:14:24 -070073 return healthd_get_property(id, val);
Todd Poynorc133b712013-08-14 17:39:13 -070074}
75
Todd Poynor020369d2013-09-18 20:09:33 -070076status_t BatteryPropertiesRegistrar::dump(int fd, const Vector<String16>& args) {
77 IPCThreadState* self = IPCThreadState::self();
78 const int pid = self->getCallingPid();
79 const int uid = self->getCallingUid();
80 if ((uid != AID_SHELL) &&
81 !PermissionCache::checkPermission(
82 String16("android.permission.DUMP"), pid, uid))
83 return PERMISSION_DENIED;
84
85 healthd_dump_battery_state(fd);
86 return OK;
87}
88
Todd Poynor752faf22013-06-12 13:25:59 -070089void BatteryPropertiesRegistrar::binderDied(const wp<IBinder>& who) {
90 Mutex::Autolock _l(mRegistrationLock);
91
92 for (size_t i = 0; i < mListeners.size(); i++) {
93 if (mListeners[i]->asBinder() == who) {
94 mListeners.removeAt(i);
95 break;
96 }
97 }
98}
99
100} // namespace android