1 /*
2  * Copyright (C) 2016 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 package android.os.connectivity;
17 
18 import static android.os.BatteryStats.NUM_WIFI_SIGNAL_STRENGTH_BINS;
19 import static android.os.BatteryStatsManager.NUM_WIFI_STATES;
20 import static android.os.BatteryStatsManager.NUM_WIFI_SUPPL_STATES;
21 
22 import android.annotation.NonNull;
23 import android.annotation.Nullable;
24 import android.annotation.SystemApi;
25 import android.os.Parcel;
26 import android.os.Parcelable;
27 
28 import java.util.Arrays;
29 import java.util.Objects;
30 
31 /**
32  * Class for holding Wifi related battery stats
33  *
34  * @hide
35  */
36 @SystemApi
37 public final class WifiBatteryStats implements Parcelable {
38     private final long mLoggingDurationMillis;
39     private final long mKernelActiveTimeMillis;
40     private final long mNumPacketsTx;
41     private final long mNumBytesTx;
42     private final long mNumPacketsRx;
43     private final long mNumBytesRx;
44     private final long mSleepTimeMillis;
45     private final long mScanTimeMillis;
46     private final long mIdleTimeMillis;
47     private final long mRxTimeMillis;
48     private final long mTxTimeMillis;
49     private final long mEnergyConsumedMaMillis;
50     private final long mAppScanRequestCount;
51     private final long[] mTimeInStateMillis;
52     private final long[] mTimeInSupplicantStateMillis;
53     private final long[] mTimeInRxSignalStrengthLevelMillis;
54     private final long mMonitoredRailChargeConsumedMaMillis;
55 
56     public static final @NonNull Parcelable.Creator<WifiBatteryStats> CREATOR =
57             new Parcelable.Creator<WifiBatteryStats>() {
58                 public WifiBatteryStats createFromParcel(Parcel in) {
59                     long loggingDurationMillis = in.readLong();
60                     long kernelActiveTimeMillis = in.readLong();
61                     long numPacketsTx = in.readLong();
62                     long numBytesTx = in.readLong();
63                     long numPacketsRx = in.readLong();
64                     long numBytesRx = in.readLong();
65                     long sleepTimeMillis = in.readLong();
66                     long scanTimeMillis = in.readLong();
67                     long idleTimeMillis = in.readLong();
68                     long rxTimeMillis = in.readLong();
69                     long txTimeMillis = in.readLong();
70                     long energyConsumedMaMillis = in.readLong();
71                     long appScanRequestCount = in.readLong();
72                     long[] timeInStateMillis = in.createLongArray();
73                     long[] timeInRxSignalStrengthLevelMillis = in.createLongArray();
74                     long[] timeInSupplicantStateMillis = in.createLongArray();
75                     long monitoredRailChargeConsumedMaMillis = in.readLong();
76                     return new WifiBatteryStats(loggingDurationMillis, kernelActiveTimeMillis,
77                             numPacketsTx, numBytesTx, numPacketsRx, numBytesRx, sleepTimeMillis,
78                             scanTimeMillis, idleTimeMillis, rxTimeMillis, txTimeMillis,
79                             energyConsumedMaMillis, appScanRequestCount, timeInStateMillis,
80                             timeInRxSignalStrengthLevelMillis, timeInSupplicantStateMillis,
81                             monitoredRailChargeConsumedMaMillis);
82                 }
83 
84                 public WifiBatteryStats[] newArray(int size) {
85                     return new WifiBatteryStats[size];
86                 }
87             };
88 
89     @Override
describeContents()90     public int describeContents() {
91         return 0;
92     }
93 
94     @Override
writeToParcel(@onNull Parcel out, int flags)95     public void writeToParcel(@NonNull Parcel out, int flags) {
96         out.writeLong(mLoggingDurationMillis);
97         out.writeLong(mKernelActiveTimeMillis);
98         out.writeLong(mNumPacketsTx);
99         out.writeLong(mNumBytesTx);
100         out.writeLong(mNumPacketsRx);
101         out.writeLong(mNumBytesRx);
102         out.writeLong(mSleepTimeMillis);
103         out.writeLong(mScanTimeMillis);
104         out.writeLong(mIdleTimeMillis);
105         out.writeLong(mRxTimeMillis);
106         out.writeLong(mTxTimeMillis);
107         out.writeLong(mEnergyConsumedMaMillis);
108         out.writeLong(mAppScanRequestCount);
109         out.writeLongArray(mTimeInStateMillis);
110         out.writeLongArray(mTimeInRxSignalStrengthLevelMillis);
111         out.writeLongArray(mTimeInSupplicantStateMillis);
112         out.writeLong(mMonitoredRailChargeConsumedMaMillis);
113     }
114 
115     @Override
equals(@ullable Object other)116     public boolean equals(@Nullable Object other) {
117         if (!(other instanceof WifiBatteryStats)) return false;
118         if (other == this) return true;
119         WifiBatteryStats otherStats = (WifiBatteryStats) other;
120         return this.mLoggingDurationMillis == otherStats.mLoggingDurationMillis
121                 && this.mKernelActiveTimeMillis == otherStats.mKernelActiveTimeMillis
122                 && this.mNumPacketsTx == otherStats.mNumPacketsTx
123                 && this.mNumBytesTx == otherStats.mNumBytesTx
124                 && this.mNumPacketsRx == otherStats.mNumPacketsRx
125                 && this.mNumBytesRx == otherStats.mNumBytesRx
126                 && this.mSleepTimeMillis == otherStats.mSleepTimeMillis
127                 && this.mScanTimeMillis == otherStats.mScanTimeMillis
128                 && this.mIdleTimeMillis == otherStats.mIdleTimeMillis
129                 && this.mRxTimeMillis == otherStats.mRxTimeMillis
130                 && this.mTxTimeMillis == otherStats.mTxTimeMillis
131                 && this.mEnergyConsumedMaMillis == otherStats.mEnergyConsumedMaMillis
132                 && this.mAppScanRequestCount == otherStats.mAppScanRequestCount
133                 && Arrays.equals(this.mTimeInStateMillis, otherStats.mTimeInStateMillis)
134                 && Arrays.equals(this.mTimeInSupplicantStateMillis,
135                     otherStats.mTimeInSupplicantStateMillis)
136                 && Arrays.equals(this.mTimeInRxSignalStrengthLevelMillis,
137                     otherStats.mTimeInRxSignalStrengthLevelMillis)
138                 && this.mMonitoredRailChargeConsumedMaMillis
139                     == otherStats.mMonitoredRailChargeConsumedMaMillis;
140     }
141 
142     @Override
hashCode()143     public int hashCode() {
144         return Objects.hash(mLoggingDurationMillis, mKernelActiveTimeMillis, mNumPacketsTx,
145                 mNumBytesTx, mNumPacketsRx, mNumBytesRx, mSleepTimeMillis, mScanTimeMillis,
146                 mIdleTimeMillis, mRxTimeMillis, mTxTimeMillis, mEnergyConsumedMaMillis,
147                 mAppScanRequestCount, Arrays.hashCode(mTimeInStateMillis),
148                 Arrays.hashCode(mTimeInSupplicantStateMillis),
149                 Arrays.hashCode(mTimeInRxSignalStrengthLevelMillis),
150                 mMonitoredRailChargeConsumedMaMillis);
151     }
152 
153     /** @hide **/
WifiBatteryStats(long loggingDurationMillis, long kernelActiveTimeMillis, long numPacketsTx, long numBytesTx, long numPacketsRx, long numBytesRx, long sleepTimeMillis, long scanTimeMillis, long idleTimeMillis, long rxTimeMillis, long txTimeMillis, long energyConsumedMaMillis, long appScanRequestCount, @NonNull long[] timeInStateMillis, @NonNull long [] timeInRxSignalStrengthLevelMillis, @NonNull long[] timeInSupplicantStateMillis, long monitoredRailChargeConsumedMaMillis)154     public WifiBatteryStats(long loggingDurationMillis, long kernelActiveTimeMillis,
155             long numPacketsTx, long numBytesTx, long numPacketsRx, long numBytesRx,
156             long sleepTimeMillis, long scanTimeMillis, long idleTimeMillis, long rxTimeMillis,
157             long txTimeMillis, long energyConsumedMaMillis, long appScanRequestCount,
158             @NonNull long[] timeInStateMillis, @NonNull long [] timeInRxSignalStrengthLevelMillis,
159             @NonNull long[] timeInSupplicantStateMillis, long monitoredRailChargeConsumedMaMillis) {
160         mLoggingDurationMillis = loggingDurationMillis;
161         mKernelActiveTimeMillis = kernelActiveTimeMillis;
162         mNumPacketsTx = numPacketsTx;
163         mNumBytesTx = numBytesTx;
164         mNumPacketsRx = numPacketsRx;
165         mNumBytesRx = numBytesRx;
166         mSleepTimeMillis = sleepTimeMillis;
167         mScanTimeMillis = scanTimeMillis;
168         mIdleTimeMillis = idleTimeMillis;
169         mRxTimeMillis = rxTimeMillis;
170         mTxTimeMillis = txTimeMillis;
171         mEnergyConsumedMaMillis = energyConsumedMaMillis;
172         mAppScanRequestCount = appScanRequestCount;
173         mTimeInStateMillis = Arrays.copyOfRange(
174                 timeInStateMillis, 0,
175                 Math.min(timeInStateMillis.length, NUM_WIFI_STATES));
176         mTimeInRxSignalStrengthLevelMillis = Arrays.copyOfRange(
177                 timeInRxSignalStrengthLevelMillis, 0,
178                 Math.min(timeInRxSignalStrengthLevelMillis.length, NUM_WIFI_SIGNAL_STRENGTH_BINS));
179         mTimeInSupplicantStateMillis = Arrays.copyOfRange(
180                 timeInSupplicantStateMillis, 0,
181                 Math.min(timeInSupplicantStateMillis.length, NUM_WIFI_SUPPL_STATES));
182         mMonitoredRailChargeConsumedMaMillis = monitoredRailChargeConsumedMaMillis;
183     }
184 
185     /**
186      * Returns the duration for which these wifi stats were collected.
187      *
188      * @return Duration of stats collection in millis.
189      */
getLoggingDurationMillis()190     public long getLoggingDurationMillis() {
191         return mLoggingDurationMillis;
192     }
193 
194     /**
195      * Returns the duration for which the kernel was active within
196      * {@link #getLoggingDurationMillis()}.
197      *
198      * @return Duration of kernel active time in millis.
199      */
getKernelActiveTimeMillis()200     public long getKernelActiveTimeMillis() {
201         return mKernelActiveTimeMillis;
202     }
203 
204     /**
205      * Returns the number of packets transmitted over wifi within
206      * {@link #getLoggingDurationMillis()}.
207      *
208      * @return Number of packets transmitted.
209      */
getNumPacketsTx()210     public long getNumPacketsTx() {
211         return mNumPacketsTx;
212     }
213 
214     /**
215      * Returns the number of bytes transmitted over wifi within
216      * {@link #getLoggingDurationMillis()}.
217      *
218      * @return Number of bytes transmitted.
219      */
getNumBytesTx()220     public long getNumBytesTx() {
221         return mNumBytesTx;
222     }
223 
224     /**
225      * Returns the number of packets received over wifi within
226      * {@link #getLoggingDurationMillis()}.
227      *
228      * @return Number of packets received.
229      */
getNumPacketsRx()230     public long getNumPacketsRx() {
231         return mNumPacketsRx;
232     }
233 
234     /**
235      * Returns the number of bytes received over wifi within
236      * {@link #getLoggingDurationMillis()}.
237      *
238      * @return Number of bytes received.
239      */
getNumBytesRx()240     public long getNumBytesRx() {
241         return mNumBytesRx;
242     }
243 
244     /**
245      * Returns the duration for which the device was sleeping within
246      * {@link #getLoggingDurationMillis()}.
247      *
248      * @return Duration of sleep time in millis.
249      */
getSleepTimeMillis()250     public long getSleepTimeMillis() {
251         return mSleepTimeMillis;
252     }
253 
254     /**
255      * Returns the duration for which the device was wifi scanning within
256      * {@link #getLoggingDurationMillis()}.
257      *
258      * @return Duration of wifi scanning time in millis.
259      */
getScanTimeMillis()260     public long getScanTimeMillis() {
261         return mScanTimeMillis;
262     }
263 
264     /**
265      * Returns the duration for which the device was idle within
266      * {@link #getLoggingDurationMillis()}.
267      *
268      * @return Duration of idle time in millis.
269      */
getIdleTimeMillis()270     public long getIdleTimeMillis() {
271         return mIdleTimeMillis;
272     }
273 
274     /**
275      * Returns the duration for which the device was receiving over wifi within
276      * {@link #getLoggingDurationMillis()}.
277      *
278      * @return Duration of wifi reception time in millis.
279      */
getRxTimeMillis()280     public long getRxTimeMillis() {
281         return mRxTimeMillis;
282     }
283 
284     /**
285      * Returns the duration for which the device was transmitting over wifi within
286      * {@link #getLoggingDurationMillis()}.
287      *
288      * @return Duration of wifi transmission time in millis.
289      */
getTxTimeMillis()290     public long getTxTimeMillis() {
291         return mTxTimeMillis;
292     }
293 
294     /**
295      * Returns an estimation of energy consumed in millis by wifi chip within
296      * {@link #getLoggingDurationMillis()}.
297      *
298      * @return Energy consumed in millis.
299      */
getEnergyConsumedMaMillis()300     public long getEnergyConsumedMaMillis() {
301         return mEnergyConsumedMaMillis;
302     }
303 
304     /**
305      * Returns the number of app initiated wifi scans within {@link #getLoggingDurationMillis()}.
306      *
307      * @return Number of app scans.
308      */
getAppScanRequestCount()309     public long getAppScanRequestCount() {
310         return mAppScanRequestCount;
311     }
312 
313     /**
314      * Returns the energy consumed by wifi chip within {@link #getLoggingDurationMillis()}.
315      *
316      * @return Energy consumed in millis.
317      */
getMonitoredRailChargeConsumedMaMillis()318     public long getMonitoredRailChargeConsumedMaMillis() {
319         return mMonitoredRailChargeConsumedMaMillis;
320     }
321 }
322