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.net.wifi;
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 wifi stack state. Timestamp the record with elapsed
27  * real-time.
28  * @hide
29  */
30 public 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 mControllerScanTimeMs;
60 
61     /**
62      * @hide
63      */
64     public long mControllerIdleTimeMs;
65 
66     /**
67      * @hide
68      */
69     public long mControllerEnergyUsed;
70 
71     public static final int STACK_STATE_INVALID = 0;
72     public static final int STACK_STATE_STATE_ACTIVE = 1;
73     public static final int STACK_STATE_STATE_SCANNING = 2;
74     public static final int STACK_STATE_STATE_IDLE = 3;
75 
WifiActivityEnergyInfo(long timestamp, int stackState, long txTime, long[] txTimePerLevel, long rxTime, long scanTime, long idleTime, long energyUsed)76     public WifiActivityEnergyInfo(long timestamp, int stackState,
77                                   long txTime, long[] txTimePerLevel, long rxTime, long scanTime,
78                                   long idleTime, long energyUsed) {
79         mTimestamp = timestamp;
80         mStackState = stackState;
81         mControllerTxTimeMs = txTime;
82         mControllerTxTimePerLevelMs = txTimePerLevel;
83         mControllerRxTimeMs = rxTime;
84         mControllerScanTimeMs = scanTime;
85         mControllerIdleTimeMs = idleTime;
86         mControllerEnergyUsed = energyUsed;
87     }
88 
89     @Override
toString()90     public String toString() {
91         return "WifiActivityEnergyInfo{"
92             + " timestamp=" + mTimestamp
93             + " mStackState=" + mStackState
94             + " mControllerTxTimeMs=" + mControllerTxTimeMs
95             + " mControllerTxTimePerLevelMs=" + Arrays.toString(mControllerTxTimePerLevelMs)
96             + " mControllerRxTimeMs=" + mControllerRxTimeMs
97             + " mControllerScanTimeMs=" + mControllerScanTimeMs
98             + " mControllerIdleTimeMs=" + mControllerIdleTimeMs
99             + " mControllerEnergyUsed=" + mControllerEnergyUsed
100             + " }";
101     }
102 
103     public static final @android.annotation.NonNull Parcelable.Creator<WifiActivityEnergyInfo> CREATOR =
104             new Parcelable.Creator<WifiActivityEnergyInfo>() {
105         public WifiActivityEnergyInfo createFromParcel(Parcel in) {
106             long timestamp = in.readLong();
107             int stackState = in.readInt();
108             long txTime = in.readLong();
109             long[] txTimePerLevel = in.createLongArray();
110             long rxTime = in.readLong();
111             long scanTime = in.readLong();
112             long idleTime = in.readLong();
113             long energyUsed = in.readLong();
114             return new WifiActivityEnergyInfo(timestamp, stackState,
115                     txTime, txTimePerLevel, rxTime, scanTime, idleTime, energyUsed);
116         }
117         public WifiActivityEnergyInfo[] newArray(int size) {
118             return new WifiActivityEnergyInfo[size];
119         }
120     };
121 
writeToParcel(Parcel out, int flags)122     public void writeToParcel(Parcel out, int flags) {
123         out.writeLong(mTimestamp);
124         out.writeInt(mStackState);
125         out.writeLong(mControllerTxTimeMs);
126         out.writeLongArray(mControllerTxTimePerLevelMs);
127         out.writeLong(mControllerRxTimeMs);
128         out.writeLong(mControllerScanTimeMs);
129         out.writeLong(mControllerIdleTimeMs);
130         out.writeLong(mControllerEnergyUsed);
131     }
132 
describeContents()133     public int describeContents() {
134         return 0;
135     }
136 
137     /**
138      * @return bt stack reported state
139      */
getStackState()140     public int getStackState() {
141         return mStackState;
142     }
143 
144     /**
145      * @return tx time in ms
146      */
getControllerTxTimeMillis()147     public long getControllerTxTimeMillis() {
148         return mControllerTxTimeMs;
149     }
150 
151     /**
152      * @return tx time at power level provided in ms
153      */
getControllerTxTimeMillisAtLevel(int level)154     public long getControllerTxTimeMillisAtLevel(int level) {
155         if (level < mControllerTxTimePerLevelMs.length) {
156             return mControllerTxTimePerLevelMs[level];
157         }
158         return 0;
159     }
160 
161     /**
162      * @return rx time in ms
163      */
getControllerRxTimeMillis()164     public long getControllerRxTimeMillis() {
165         return mControllerRxTimeMs;
166     }
167 
168     /**
169      * @return scan time in ms
170      */
getControllerScanTimeMillis()171     public long getControllerScanTimeMillis() {
172         return mControllerScanTimeMs;
173     }
174 
175     /**
176      * @return idle time in ms
177      */
getControllerIdleTimeMillis()178     public long getControllerIdleTimeMillis() {
179         return mControllerIdleTimeMs;
180     }
181 
182     /**
183      * product of current(mA), voltage(V) and time(ms)
184      * @return energy used
185      */
getControllerEnergyUsed()186     public long getControllerEnergyUsed() {
187         return mControllerEnergyUsed;
188     }
189     /**
190      * @return timestamp(wall clock) of record creation
191      */
getTimeStamp()192     public long getTimeStamp() {
193         return mTimestamp;
194     }
195 
196     /**
197      * @return if the record is valid
198      */
isValid()199     public boolean isValid() {
200         return ((mControllerTxTimeMs >=0) &&
201                 (mControllerRxTimeMs >=0) &&
202                 (mControllerScanTimeMs >=0) &&
203                 (mControllerIdleTimeMs >=0));
204     }
205 }