Home | History | Annotate | Download | only in wifi
      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 com.android.server.wifi;
     17 
     18 import android.os.BatteryStats;
     19 import android.os.RemoteException;
     20 import android.os.ServiceManager;
     21 import android.os.connectivity.WifiBatteryStats;
     22 import android.text.format.DateUtils;
     23 import android.util.Log;
     24 
     25 import com.android.internal.app.IBatteryStats;
     26 import com.android.server.wifi.nano.WifiMetricsProto.WifiPowerStats;
     27 
     28 import java.io.PrintWriter;
     29 
     30 /**
     31  * WifiPowerMetrics holds the wifi power metrics and converts them to WifiPowerStats proto buf.
     32  * This proto buf is included in the Wifi proto buf.
     33  */
     34 public class WifiPowerMetrics {
     35 
     36     private static final String TAG = "WifiPowerMetrics";
     37 
     38     /* BatteryStats API */
     39     private final IBatteryStats mBatteryStats;
     40 
     41     public WifiPowerMetrics() {
     42         mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
     43             BatteryStats.SERVICE_NAME));
     44     }
     45 
     46     /**
     47      * Build WifiPowerStats proto
     48      * A snapshot of Wifi statistics in Batterystats is obtained. Due to reboots multiple correlated
     49      * logs may be uploaded in a day. Resolution is on the server side. The log with longest
     50      * duration is picked.
     51      * @return WifiPowerStats
     52      */
     53     public WifiPowerStats buildProto() {
     54         WifiPowerStats m = new WifiPowerStats();
     55         WifiBatteryStats stats = getStats();
     56         if (stats != null) {
     57             m.loggingDurationMs = stats.getLoggingDurationMs();
     58             m.energyConsumedMah = stats.getEnergyConsumedMaMs()
     59                 / ((double) DateUtils.HOUR_IN_MILLIS);
     60             m.idleTimeMs = stats.getIdleTimeMs();
     61             m.rxTimeMs = stats.getRxTimeMs();
     62             m.txTimeMs = stats.getTxTimeMs();
     63         }
     64         return m;
     65     }
     66 
     67     /**
     68      * Dump all WifiPowerStats to console (pw)
     69      * @param pw
     70      */
     71     public void dump(PrintWriter pw) {
     72         WifiPowerStats s = buildProto();
     73         if (s!=null) {
     74             pw.println("Wifi power metrics:");
     75             pw.println("Logging duration (time on battery): " + s.loggingDurationMs);
     76             pw.println("Energy consumed by wifi (mAh): " + s.energyConsumedMah);
     77             pw.println("Amount of time wifi is in idle (ms): " + s.idleTimeMs);
     78             pw.println("Amount of time wifi is in rx (ms): " + s.rxTimeMs);
     79             pw.println("Amount of time wifi is in tx (ms): " + s.txTimeMs);
     80         }
     81     }
     82 
     83     /**
     84      * Get wifi stats from batterystats
     85      * @return WifiBatteryStats
     86      */
     87     private WifiBatteryStats getStats() {
     88         try {
     89             return mBatteryStats.getWifiBatteryStats();
     90         } catch (RemoteException e) {
     91             Log.e(TAG, "Unable to obtain Wifi power stats from BatteryStats");
     92         }
     93         return null;
     94     }
     95 }
     96