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     explicit 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         void scheduleUpdate() {
     65             Parcel data;
     66             data.writeInterfaceToken(IBatteryPropertiesRegistrar::getInterfaceDescriptor());
     67             remote()->transact(SCHEDULE_UPDATE, data, NULL);
     68         }
     69 };
     70 
     71 IMPLEMENT_META_INTERFACE(BatteryPropertiesRegistrar, "android.os.IBatteryPropertiesRegistrar");
     72 
     73 status_t BnBatteryPropertiesRegistrar::onTransact(uint32_t code,
     74                                                   const Parcel& data,
     75                                                   Parcel* reply,
     76                                                   uint32_t flags)
     77 {
     78     switch(code) {
     79         case REGISTER_LISTENER: {
     80             CHECK_INTERFACE(IBatteryPropertiesRegistrar, data, reply);
     81             sp<IBatteryPropertiesListener> listener =
     82                 interface_cast<IBatteryPropertiesListener>(data.readStrongBinder());
     83             registerListener(listener);
     84             return OK;
     85         }
     86 
     87         case UNREGISTER_LISTENER: {
     88             CHECK_INTERFACE(IBatteryPropertiesRegistrar, data, reply);
     89             sp<IBatteryPropertiesListener> listener =
     90                 interface_cast<IBatteryPropertiesListener>(data.readStrongBinder());
     91             unregisterListener(listener);
     92             return OK;
     93         }
     94 
     95         case GET_PROPERTY: {
     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);
    104             return OK;
    105         }
    106 
    107         case SCHEDULE_UPDATE: {
    108             CHECK_INTERFACE(IBatteryPropertiesRegistrar, data, reply);
    109             scheduleUpdate();
    110             return OK;
    111         }
    112     }
    113     return BBinder::onTransact(code, data, reply, flags);
    114 };
    115 
    116 // ----------------------------------------------------------------------------
    117 
    118 }; // namespace android
    119