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 | |
Yabin Cui | e98e177 | 2016-02-17 12:21:34 -0800 | [diff] [blame] | 29 | #include <healthd/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 | |
Todd Poynor | e553564 | 2016-01-05 19:41:34 -0800 | [diff] [blame] | 33 | void BatteryPropertiesRegistrar::publish( |
| 34 | const sp<BatteryPropertiesRegistrar>& service) { |
| 35 | defaultServiceManager()->addService(String16("batteryproperties"), service); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Chih-Hung Hsieh | 4df1056 | 2016-07-27 16:05:50 -0700 | [diff] [blame] | 38 | void BatteryPropertiesRegistrar::notifyListeners(const struct BatteryProperties& props) { |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 39 | Mutex::Autolock _l(mRegistrationLock); |
| 40 | for (size_t i = 0; i < mListeners.size(); i++) { |
| 41 | mListeners[i]->batteryPropertiesChanged(props); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void BatteryPropertiesRegistrar::registerListener(const sp<IBatteryPropertiesListener>& listener) { |
| 46 | { |
Natalie Silvanovich | 3e6d331 | 2014-04-30 14:50:27 -0700 | [diff] [blame] | 47 | if (listener == NULL) |
| 48 | return; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 49 | Mutex::Autolock _l(mRegistrationLock); |
| 50 | // check whether this is a duplicate |
| 51 | for (size_t i = 0; i < mListeners.size(); i++) { |
Marco Nelissen | 26279a9 | 2014-11-14 08:03:02 -0800 | [diff] [blame] | 52 | if (IInterface::asBinder(mListeners[i]) == IInterface::asBinder(listener)) { |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 53 | return; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | mListeners.add(listener); |
Marco Nelissen | 26279a9 | 2014-11-14 08:03:02 -0800 | [diff] [blame] | 58 | IInterface::asBinder(listener)->linkToDeath(this); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 59 | } |
Todd Poynor | 7b27f27 | 2013-09-06 15:14:24 -0700 | [diff] [blame] | 60 | healthd_battery_update(); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void BatteryPropertiesRegistrar::unregisterListener(const sp<IBatteryPropertiesListener>& listener) { |
Natalie Silvanovich | 3e6d331 | 2014-04-30 14:50:27 -0700 | [diff] [blame] | 64 | if (listener == NULL) |
| 65 | return; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 66 | Mutex::Autolock _l(mRegistrationLock); |
| 67 | for (size_t i = 0; i < mListeners.size(); i++) { |
Marco Nelissen | 26279a9 | 2014-11-14 08:03:02 -0800 | [diff] [blame] | 68 | if (IInterface::asBinder(mListeners[i]) == IInterface::asBinder(listener)) { |
| 69 | IInterface::asBinder(mListeners[i])->unlinkToDeath(this); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 70 | mListeners.removeAt(i); |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 76 | status_t BatteryPropertiesRegistrar::getProperty(int id, struct BatteryProperty *val) { |
Todd Poynor | 7b27f27 | 2013-09-06 15:14:24 -0700 | [diff] [blame] | 77 | return healthd_get_property(id, val); |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Adam Lesinski | 3dec1aa | 2017-02-15 18:49:00 -0800 | [diff] [blame] | 80 | void BatteryPropertiesRegistrar::scheduleUpdate() { |
| 81 | healthd_battery_update(); |
| 82 | } |
| 83 | |
Mark Salyzyn | 6f5b47f | 2014-05-15 15:00:59 -0700 | [diff] [blame] | 84 | status_t BatteryPropertiesRegistrar::dump(int fd, const Vector<String16>& /*args*/) { |
Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 85 | IPCThreadState* self = IPCThreadState::self(); |
| 86 | const int pid = self->getCallingPid(); |
| 87 | const int uid = self->getCallingUid(); |
| 88 | if ((uid != AID_SHELL) && |
| 89 | !PermissionCache::checkPermission( |
| 90 | String16("android.permission.DUMP"), pid, uid)) |
| 91 | return PERMISSION_DENIED; |
| 92 | |
| 93 | healthd_dump_battery_state(fd); |
| 94 | return OK; |
| 95 | } |
| 96 | |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 97 | void BatteryPropertiesRegistrar::binderDied(const wp<IBinder>& who) { |
| 98 | Mutex::Autolock _l(mRegistrationLock); |
| 99 | |
| 100 | for (size_t i = 0; i < mListeners.size(); i++) { |
Marco Nelissen | 26279a9 | 2014-11-14 08:03:02 -0800 | [diff] [blame] | 101 | if (IInterface::asBinder(mListeners[i]) == who) { |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 102 | mListeners.removeAt(i); |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | } // namespace android |