blob: 46934e04e17e807b8581476b1cabb34bf5588106 [file] [log] [blame]
Todd Poynor87bf0d92013-05-16 14:51:15 -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#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
27namespace android {
28
29class BpBatteryPropertiesRegistrar : public BpInterface<IBatteryPropertiesRegistrar> {
30public:
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());
Marco Nelissen2ea926b2014-11-14 08:01:01 -080037 data.writeStrongBinder(IInterface::asBinder(listener));
Todd Poynor87bf0d92013-05-16 14:51:15 -070038 remote()->transact(REGISTER_LISTENER, data, NULL);
39 }
40
41 void unregisterListener(const sp<IBatteryPropertiesListener>& listener) {
42 Parcel data;
43 data.writeInterfaceToken(IBatteryPropertiesRegistrar::getInterfaceDescriptor());
Marco Nelissen2ea926b2014-11-14 08:01:01 -080044 data.writeStrongBinder(IInterface::asBinder(listener));
Todd Poynor87bf0d92013-05-16 14:51:15 -070045 remote()->transact(UNREGISTER_LISTENER, data, NULL);
46 }
Todd Poynorcf808732013-08-14 17:23:37 -070047
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);
Paul Lawrence0a324612014-03-19 14:47:29 -070053 int32_t ret = reply.readExceptionCode();
54 if (ret != 0) {
55 return ret;
56 }
57 ret = reply.readInt32();
Todd Poynorcf808732013-08-14 17:23:37 -070058 int parcelpresent = reply.readInt32();
59 if (parcelpresent)
60 val->readFromParcel(&reply);
61 return ret;
62 }
Todd Poynor87bf0d92013-05-16 14:51:15 -070063};
64
65IMPLEMENT_META_INTERFACE(BatteryPropertiesRegistrar, "android.os.IBatteryPropertiesRegistrar");
66
67status_t BnBatteryPropertiesRegistrar::onTransact(uint32_t code,
68 const Parcel& data,
69 Parcel* reply,
70 uint32_t flags)
71{
72 switch(code) {
73 case REGISTER_LISTENER: {
74 CHECK_INTERFACE(IBatteryPropertiesRegistrar, data, reply);
75 sp<IBatteryPropertiesListener> listener =
76 interface_cast<IBatteryPropertiesListener>(data.readStrongBinder());
77 registerListener(listener);
78 return OK;
79 }
80
81 case UNREGISTER_LISTENER: {
82 CHECK_INTERFACE(IBatteryPropertiesRegistrar, data, reply);
83 sp<IBatteryPropertiesListener> listener =
84 interface_cast<IBatteryPropertiesListener>(data.readStrongBinder());
85 unregisterListener(listener);
86 return OK;
87 }
Todd Poynorcf808732013-08-14 17:23:37 -070088
89 case GET_PROPERTY: {
90 CHECK_INTERFACE(IBatteryPropertiesRegistrar, data, reply);
91 int id = data.readInt32();
92 struct BatteryProperty val;
93 status_t result = getProperty(id, &val);
94 reply->writeNoException();
95 reply->writeInt32(result);
96 reply->writeInt32(1);
97 val.writeToParcel(reply);
98 return OK;
99 }
Todd Poynor87bf0d92013-05-16 14:51:15 -0700100 }
101 return BBinder::onTransact(code, data, reply, flags);
102};
103
104// ----------------------------------------------------------------------------
105
106}; // namespace android