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
17package android.net.wifi;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22import java.util.Arrays;
23
24/**
25 * Record of energy and activity information from controller and
26 * underlying wifi stack state. Timestamp the record with elapsed
27 * real-time.
28 * @hide
29 */
30public final class WifiActivityEnergyInfo implements Parcelable {
31    /**
32     * @hide
33     */
34    public long mTimestamp;
35
36    /**
37     * @hide
38     */
39    public int mStackState;
40
41    /**
42     * @hide
43     */
44    public long mControllerTxTimeMs;
45
46    /**
47     * @hide
48     */
49    public long[] mControllerTxTimePerLevelMs;
50
51    /**
52     * @hide
53     */
54    public long mControllerRxTimeMs;
55
56    /**
57     * @hide
58     */
59    public long mControllerIdleTimeMs;
60
61    /**
62     * @hide
63     */
64    public long mControllerEnergyUsed;
65
66    public static final int STACK_STATE_INVALID = 0;
67    public static final int STACK_STATE_STATE_ACTIVE = 1;
68    public static final int STACK_STATE_STATE_SCANNING = 2;
69    public static final int STACK_STATE_STATE_IDLE = 3;
70
71    public WifiActivityEnergyInfo(long timestamp, int stackState,
72                                  long txTime, long[] txTimePerLevel, long rxTime, long idleTime,
73                                  long energyUsed) {
74        mTimestamp = timestamp;
75        mStackState = stackState;
76        mControllerTxTimeMs = txTime;
77        mControllerTxTimePerLevelMs = txTimePerLevel;
78        mControllerRxTimeMs = rxTime;
79        mControllerIdleTimeMs = idleTime;
80        mControllerEnergyUsed = energyUsed;
81    }
82
83    @Override
84    public String toString() {
85        return "WifiActivityEnergyInfo{"
86            + " timestamp=" + mTimestamp
87            + " mStackState=" + mStackState
88            + " mControllerTxTimeMs=" + mControllerTxTimeMs
89            + " mControllerTxTimePerLevelMs=" + Arrays.toString(mControllerTxTimePerLevelMs)
90            + " mControllerRxTimeMs=" + mControllerRxTimeMs
91            + " mControllerIdleTimeMs=" + mControllerIdleTimeMs
92            + " mControllerEnergyUsed=" + mControllerEnergyUsed
93            + " }";
94    }
95
96    public static final Parcelable.Creator<WifiActivityEnergyInfo> CREATOR =
97            new Parcelable.Creator<WifiActivityEnergyInfo>() {
98        public WifiActivityEnergyInfo createFromParcel(Parcel in) {
99            long timestamp = in.readLong();
100            int stackState = in.readInt();
101            long txTime = in.readLong();
102            long[] txTimePerLevel = in.createLongArray();
103            long rxTime = in.readLong();
104            long idleTime = in.readLong();
105            long energyUsed = in.readLong();
106            return new WifiActivityEnergyInfo(timestamp, stackState,
107                    txTime, txTimePerLevel, rxTime, idleTime, energyUsed);
108        }
109        public WifiActivityEnergyInfo[] newArray(int size) {
110            return new WifiActivityEnergyInfo[size];
111        }
112    };
113
114    public void writeToParcel(Parcel out, int flags) {
115        out.writeLong(mTimestamp);
116        out.writeInt(mStackState);
117        out.writeLong(mControllerTxTimeMs);
118        out.writeLongArray(mControllerTxTimePerLevelMs);
119        out.writeLong(mControllerRxTimeMs);
120        out.writeLong(mControllerIdleTimeMs);
121        out.writeLong(mControllerEnergyUsed);
122    }
123
124    public int describeContents() {
125        return 0;
126    }
127
128    /**
129     * @return bt stack reported state
130     */
131    public int getStackState() {
132        return mStackState;
133    }
134
135    /**
136     * @return tx time in ms
137     */
138    public long getControllerTxTimeMillis() {
139        return mControllerTxTimeMs;
140    }
141
142    /**
143     * @return tx time at power level provided in ms
144     */
145    public long getControllerTxTimeMillisAtLevel(int level) {
146        if (level < mControllerTxTimePerLevelMs.length) {
147            return mControllerTxTimePerLevelMs[level];
148        }
149        return 0;
150    }
151
152    /**
153     * @return rx time in ms
154     */
155    public long getControllerRxTimeMillis() {
156        return mControllerRxTimeMs;
157    }
158
159    /**
160     * @return idle time in ms
161     */
162    public long getControllerIdleTimeMillis() {
163        return mControllerIdleTimeMs;
164    }
165
166    /**
167     * product of current(mA), voltage(V) and time(ms)
168     * @return energy used
169     */
170    public long getControllerEnergyUsed() {
171        return mControllerEnergyUsed;
172    }
173    /**
174     * @return timestamp(wall clock) of record creation
175     */
176    public long getTimeStamp() {
177        return mTimestamp;
178    }
179
180    /**
181     * @return if the record is valid
182     */
183    public boolean isValid() {
184        return ((mControllerTxTimeMs >=0) &&
185                (mControllerRxTimeMs >=0) &&
186                (mControllerIdleTimeMs >=0));
187    }
188}
189