Home | History | Annotate | Download | only in batteryservice
      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(IInterface::asBinder(listener));
     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(IInterface::asBinder(listener));
     45             remote()->transact(UNREGISTER_LISTENER, data, NULL);
     46         }
     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             int32_t ret = reply.readExceptionCode();
     54             if (ret != 0) {
     55                 return ret;
     56             }
     57             ret = reply.readInt32();
     58             int parcelpresent = reply.readInt32();
     59             if (parcelpresent)
     60                 val->readFromParcel(&reply);
     61             return ret;
     62         }
     63 };
     64 
     65 IMPLEMENT_META_INTERFACE(BatteryPropertiesRegistrar, "android.os.IBatteryPropertiesRegistrar");
     66 
     67 status_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         }
     88 
     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         }
    100     }
    101     return BBinder::onTransact(code, data, reply, flags);
    102 };
    103 
    104 // ----------------------------------------------------------------------------
    105 
    106 }; // namespace android
    107