Home | History | Annotate | Download | only in bluetooth
      1 /*
      2  * Copyright (C) 2014 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 package android.bluetooth;
     18 
     19 import android.os.Parcel;
     20 import android.os.Parcelable;
     21 
     22 import java.util.Arrays;
     23 
     24 /**
     25  * Record of energy and activity information from controller and
     26  * underlying bt stack state.Timestamp the record with system
     27  * time
     28  * @hide
     29  */
     30 public final class BluetoothActivityEnergyInfo implements Parcelable {
     31     private final long mTimestamp;
     32     private int mBluetoothStackState;
     33     private long mControllerTxTimeMs;
     34     private long mControllerRxTimeMs;
     35     private long mControllerIdleTimeMs;
     36     private long mControllerEnergyUsed;
     37     private UidTraffic[] mUidTraffic;
     38 
     39     public static final int BT_STACK_STATE_INVALID = 0;
     40     public static final int BT_STACK_STATE_STATE_ACTIVE = 1;
     41     public static final int BT_STACK_STATE_STATE_SCANNING = 2;
     42     public static final int BT_STACK_STATE_STATE_IDLE = 3;
     43 
     44     public BluetoothActivityEnergyInfo(long timestamp, int stackState,
     45                                        long txTime, long rxTime, long idleTime, long energyUsed) {
     46         mTimestamp = timestamp;
     47         mBluetoothStackState = stackState;
     48         mControllerTxTimeMs = txTime;
     49         mControllerRxTimeMs = rxTime;
     50         mControllerIdleTimeMs = idleTime;
     51         mControllerEnergyUsed = energyUsed;
     52     }
     53 
     54     @SuppressWarnings("unchecked")
     55     BluetoothActivityEnergyInfo(Parcel in) {
     56         mTimestamp = in.readLong();
     57         mBluetoothStackState = in.readInt();
     58         mControllerTxTimeMs = in.readLong();
     59         mControllerRxTimeMs = in.readLong();
     60         mControllerIdleTimeMs = in.readLong();
     61         mControllerEnergyUsed = in.readLong();
     62         mUidTraffic = in.createTypedArray(UidTraffic.CREATOR);
     63     }
     64 
     65     @Override
     66     public String toString() {
     67         return "BluetoothActivityEnergyInfo{"
     68             + " mTimestamp=" + mTimestamp
     69             + " mBluetoothStackState=" + mBluetoothStackState
     70             + " mControllerTxTimeMs=" + mControllerTxTimeMs
     71             + " mControllerRxTimeMs=" + mControllerRxTimeMs
     72             + " mControllerIdleTimeMs=" + mControllerIdleTimeMs
     73             + " mControllerEnergyUsed=" + mControllerEnergyUsed
     74             + " mUidTraffic=" + Arrays.toString(mUidTraffic)
     75             + " }";
     76     }
     77 
     78     public static final Parcelable.Creator<BluetoothActivityEnergyInfo> CREATOR =
     79             new Parcelable.Creator<BluetoothActivityEnergyInfo>() {
     80         public BluetoothActivityEnergyInfo createFromParcel(Parcel in) {
     81             return new BluetoothActivityEnergyInfo(in);
     82         }
     83 
     84         public BluetoothActivityEnergyInfo[] newArray(int size) {
     85             return new BluetoothActivityEnergyInfo[size];
     86         }
     87     };
     88 
     89     @SuppressWarnings("unchecked")
     90     public void writeToParcel(Parcel out, int flags) {
     91         out.writeLong(mTimestamp);
     92         out.writeInt(mBluetoothStackState);
     93         out.writeLong(mControllerTxTimeMs);
     94         out.writeLong(mControllerRxTimeMs);
     95         out.writeLong(mControllerIdleTimeMs);
     96         out.writeLong(mControllerEnergyUsed);
     97         out.writeTypedArray(mUidTraffic, flags);
     98     }
     99 
    100     public int describeContents() {
    101         return 0;
    102     }
    103 
    104     /**
    105      * @return bt stack reported state
    106      */
    107     public int getBluetoothStackState() {
    108         return mBluetoothStackState;
    109     }
    110 
    111     /**
    112      * @return tx time in ms
    113      */
    114     public long getControllerTxTimeMillis() {
    115         return mControllerTxTimeMs;
    116     }
    117 
    118     /**
    119      * @return rx time in ms
    120      */
    121     public long getControllerRxTimeMillis() {
    122         return mControllerRxTimeMs;
    123     }
    124 
    125     /**
    126      * @return idle time in ms
    127      */
    128     public long getControllerIdleTimeMillis() {
    129         return mControllerIdleTimeMs;
    130     }
    131 
    132     /**
    133      * product of current(mA), voltage(V) and time(ms)
    134      * @return energy used
    135      */
    136     public long getControllerEnergyUsed() {
    137         return mControllerEnergyUsed;
    138     }
    139 
    140     /**
    141      * @return timestamp(real time elapsed in milliseconds since boot) of record creation.
    142      */
    143     public long getTimeStamp() {
    144         return mTimestamp;
    145     }
    146 
    147     public UidTraffic[] getUidTraffic() {
    148         return mUidTraffic;
    149     }
    150 
    151     public void setUidTraffic(UidTraffic[] traffic) {
    152         mUidTraffic = traffic;
    153     }
    154 
    155     /**
    156      * @return if the record is valid
    157      */
    158     public boolean isValid() {
    159         return ((mControllerTxTimeMs >=0) &&
    160                 (mControllerRxTimeMs >=0) &&
    161                 (mControllerIdleTimeMs >=0));
    162     }
    163 }
    164