2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "IBatteryPropertiesRegistrar"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
21 #include <batteryservice/IBatteryPropertiesListener.h>
22 #include <batteryservice/IBatteryPropertiesRegistrar.h>
24 #include <sys/types.h>
25 #include <binder/Parcel.h>
29 class BpBatteryPropertiesRegistrar : public BpInterface<IBatteryPropertiesRegistrar> {
31 explicit BpBatteryPropertiesRegistrar(const sp<IBinder>& impl)
32 : BpInterface<IBatteryPropertiesRegistrar>(impl) {}
34 void registerListener(const sp<IBatteryPropertiesListener>& listener) {
36 data.writeInterfaceToken(IBatteryPropertiesRegistrar::getInterfaceDescriptor());
37 data.writeStrongBinder(IInterface::asBinder(listener));
38 remote()->transact(REGISTER_LISTENER, data, NULL);
41 void unregisterListener(const sp<IBatteryPropertiesListener>& listener) {
43 data.writeInterfaceToken(IBatteryPropertiesRegistrar::getInterfaceDescriptor());
44 data.writeStrongBinder(IInterface::asBinder(listener));
45 remote()->transact(UNREGISTER_LISTENER, data, NULL);
48 status_t getProperty(int id, struct BatteryProperty *val) {
50 data.writeInterfaceToken(IBatteryPropertiesRegistrar::getInterfaceDescriptor());
52 remote()->transact(GET_PROPERTY, data, &reply);
53 int32_t ret = reply.readExceptionCode();
57 ret = reply.readInt32();
58 int parcelpresent = reply.readInt32();
60 val->readFromParcel(&reply);
64 void scheduleUpdate() {
66 data.writeInterfaceToken(IBatteryPropertiesRegistrar::getInterfaceDescriptor());
67 remote()->transact(SCHEDULE_UPDATE, data, NULL);
71 IMPLEMENT_META_INTERFACE(BatteryPropertiesRegistrar, "android.os.IBatteryPropertiesRegistrar");
73 status_t BnBatteryPropertiesRegistrar::onTransact(uint32_t code,
79 case REGISTER_LISTENER: {
80 CHECK_INTERFACE(IBatteryPropertiesRegistrar, data, reply);
81 sp<IBatteryPropertiesListener> listener =
82 interface_cast<IBatteryPropertiesListener>(data.readStrongBinder());
83 registerListener(listener);
87 case UNREGISTER_LISTENER: {
88 CHECK_INTERFACE(IBatteryPropertiesRegistrar, data, reply);
89 sp<IBatteryPropertiesListener> listener =
90 interface_cast<IBatteryPropertiesListener>(data.readStrongBinder());
91 unregisterListener(listener);
96 CHECK_INTERFACE(IBatteryPropertiesRegistrar, data, reply);
97 int id = data.readInt32();
98 struct BatteryProperty val;
99 status_t result = getProperty(id, &val);
100 reply->writeNoException();
101 reply->writeInt32(result);
102 reply->writeInt32(1);
103 val.writeToParcel(reply);
107 case SCHEDULE_UPDATE: {
108 CHECK_INTERFACE(IBatteryPropertiesRegistrar, data, reply);
113 return BBinder::onTransact(code, data, reply, flags);
116 // ----------------------------------------------------------------------------
118 }; // namespace android