Todd Poynor | 87bf0d9 | 2013-05-16 14:51:15 -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 | #define LOG_TAG "IBatteryPropertiesRegistrar" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <batteryservice/IBatteryPropertiesListener.h> |
| 22 | #include <batteryservice/IBatteryPropertiesRegistrar.h> |
| 23 | #include <stdint.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <binder/Parcel.h> |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | class BpBatteryPropertiesRegistrar : public BpInterface<IBatteryPropertiesRegistrar> { |
| 30 | public: |
| 31 | BpBatteryPropertiesRegistrar(const sp<IBinder>& impl) |
| 32 | : BpInterface<IBatteryPropertiesRegistrar>(impl) {} |
| 33 | |
| 34 | void registerListener(const sp<IBatteryPropertiesListener>& listener) { |
| 35 | Parcel data; |
| 36 | data.writeInterfaceToken(IBatteryPropertiesRegistrar::getInterfaceDescriptor()); |
| 37 | data.writeStrongBinder(listener->asBinder()); |
| 38 | remote()->transact(REGISTER_LISTENER, data, NULL); |
| 39 | } |
| 40 | |
| 41 | void unregisterListener(const sp<IBatteryPropertiesListener>& listener) { |
| 42 | Parcel data; |
| 43 | data.writeInterfaceToken(IBatteryPropertiesRegistrar::getInterfaceDescriptor()); |
| 44 | data.writeStrongBinder(listener->asBinder()); |
| 45 | remote()->transact(UNREGISTER_LISTENER, data, NULL); |
| 46 | } |
Todd Poynor | cf80873 | 2013-08-14 17:23:37 -0700 | [diff] [blame] | 47 | |
| 48 | status_t getProperty(int id, struct BatteryProperty *val) { |
| 49 | Parcel data, reply; |
| 50 | data.writeInterfaceToken(IBatteryPropertiesRegistrar::getInterfaceDescriptor()); |
| 51 | data.writeInt32(id); |
| 52 | remote()->transact(GET_PROPERTY, data, &reply); |
| 53 | status_t ret = reply.readInt32(); |
| 54 | int parcelpresent = reply.readInt32(); |
| 55 | if (parcelpresent) |
| 56 | val->readFromParcel(&reply); |
| 57 | return ret; |
| 58 | } |
Todd Poynor | 87bf0d9 | 2013-05-16 14:51:15 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | IMPLEMENT_META_INTERFACE(BatteryPropertiesRegistrar, "android.os.IBatteryPropertiesRegistrar"); |
| 62 | |
| 63 | status_t BnBatteryPropertiesRegistrar::onTransact(uint32_t code, |
| 64 | const Parcel& data, |
| 65 | Parcel* reply, |
| 66 | uint32_t flags) |
| 67 | { |
| 68 | switch(code) { |
| 69 | case REGISTER_LISTENER: { |
| 70 | CHECK_INTERFACE(IBatteryPropertiesRegistrar, data, reply); |
| 71 | sp<IBatteryPropertiesListener> listener = |
| 72 | interface_cast<IBatteryPropertiesListener>(data.readStrongBinder()); |
| 73 | registerListener(listener); |
| 74 | return OK; |
| 75 | } |
| 76 | |
| 77 | case UNREGISTER_LISTENER: { |
| 78 | CHECK_INTERFACE(IBatteryPropertiesRegistrar, data, reply); |
| 79 | sp<IBatteryPropertiesListener> listener = |
| 80 | interface_cast<IBatteryPropertiesListener>(data.readStrongBinder()); |
| 81 | unregisterListener(listener); |
| 82 | return OK; |
| 83 | } |
Todd Poynor | cf80873 | 2013-08-14 17:23:37 -0700 | [diff] [blame] | 84 | |
| 85 | case GET_PROPERTY: { |
| 86 | CHECK_INTERFACE(IBatteryPropertiesRegistrar, data, reply); |
| 87 | int id = data.readInt32(); |
| 88 | struct BatteryProperty val; |
| 89 | status_t result = getProperty(id, &val); |
| 90 | reply->writeNoException(); |
| 91 | reply->writeInt32(result); |
| 92 | reply->writeInt32(1); |
| 93 | val.writeToParcel(reply); |
| 94 | return OK; |
| 95 | } |
Todd Poynor | 87bf0d9 | 2013-05-16 14:51:15 -0700 | [diff] [blame] | 96 | } |
| 97 | return BBinder::onTransact(code, data, reply, flags); |
| 98 | }; |
| 99 | |
| 100 | // ---------------------------------------------------------------------------- |
| 101 | |
| 102 | }; // namespace android |