Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 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> |
Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 21 | #include <binder/IPCThreadState.h> |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 22 | #include <binder/IServiceManager.h> |
Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 23 | #include <binder/PermissionCache.h> |
| 24 | #include <private/android_filesystem_config.h> |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 25 | #include <utils/Errors.h> |
| 26 | #include <utils/Mutex.h> |
| 27 | #include <utils/String16.h> |
| 28 | |
Todd Poynor | 7b27f27 | 2013-09-06 15:14:24 -0700 | [diff] [blame] | 29 | #include "healthd.h" |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 30 | |
Todd Poynor | 7b27f27 | 2013-09-06 15:14:24 -0700 | [diff] [blame] | 31 | namespace android { |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 32 | |
| 33 | void BatteryPropertiesRegistrar::publish() { |
Todd Poynor | f0e2ac2 | 2013-10-22 17:50:59 -0700 | [diff] [blame] | 34 | defaultServiceManager()->addService(String16("batteryproperties"), this); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void 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 | |
| 44 | void BatteryPropertiesRegistrar::registerListener(const sp<IBatteryPropertiesListener>& listener) { |
| 45 | { |
| 46 | Mutex::Autolock _l(mRegistrationLock); |
| 47 | // check whether this is a duplicate |
| 48 | for (size_t i = 0; i < mListeners.size(); i++) { |
| 49 | if (mListeners[i]->asBinder() == listener->asBinder()) { |
| 50 | return; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | mListeners.add(listener); |
| 55 | listener->asBinder()->linkToDeath(this); |
| 56 | } |
Todd Poynor | 7b27f27 | 2013-09-06 15:14:24 -0700 | [diff] [blame] | 57 | healthd_battery_update(); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void BatteryPropertiesRegistrar::unregisterListener(const sp<IBatteryPropertiesListener>& listener) { |
| 61 | Mutex::Autolock _l(mRegistrationLock); |
| 62 | for (size_t i = 0; i < mListeners.size(); i++) { |
| 63 | if (mListeners[i]->asBinder() == listener->asBinder()) { |
| 64 | mListeners[i]->asBinder()->unlinkToDeath(this); |
| 65 | mListeners.removeAt(i); |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 71 | status_t BatteryPropertiesRegistrar::getProperty(int id, struct BatteryProperty *val) { |
Todd Poynor | 7b27f27 | 2013-09-06 15:14:24 -0700 | [diff] [blame] | 72 | return healthd_get_property(id, val); |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 75 | status_t BatteryPropertiesRegistrar::dump(int fd, const Vector<String16>& args) { |
| 76 | IPCThreadState* self = IPCThreadState::self(); |
| 77 | const int pid = self->getCallingPid(); |
| 78 | const int uid = self->getCallingUid(); |
| 79 | if ((uid != AID_SHELL) && |
| 80 | !PermissionCache::checkPermission( |
| 81 | String16("android.permission.DUMP"), pid, uid)) |
| 82 | return PERMISSION_DENIED; |
| 83 | |
| 84 | healthd_dump_battery_state(fd); |
| 85 | return OK; |
| 86 | } |
| 87 | |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 88 | void BatteryPropertiesRegistrar::binderDied(const wp<IBinder>& who) { |
| 89 | Mutex::Autolock _l(mRegistrationLock); |
| 90 | |
| 91 | for (size_t i = 0; i < mListeners.size(); i++) { |
| 92 | if (mListeners[i]->asBinder() == who) { |
| 93 | mListeners.removeAt(i); |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | } // namespace android |