Home | History | Annotate | Download | only in rule
      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 
     17 package com.android.loganalysis.rule;
     18 
     19 import com.android.loganalysis.item.BugreportItem;
     20 import com.android.loganalysis.item.DumpsysWifiStatsItem;
     21 
     22 import java.util.concurrent.TimeUnit;
     23 
     24 import org.json.JSONException;
     25 import org.json.JSONObject;
     26 
     27 /**
     28  * Rules definition for Wifi stats
     29  */
     30 public class WifiStatsRule extends AbstractPowerRule {
     31 
     32     private static final String WIFI_STATS = "WIFI_STATS";
     33     private static final int WIFI_DISCONNECT_THRESHOLD = 1; // wifi disconnect should never happen
     34     private static final int WIFI_ASSOCIATION_THRESHOLD = 1;
     35     // Wifi scans are scheduled by GSA every 285 seconds, anything more frequent is an issue
     36     private static final long WIFI_SCAN_INTERVAL_THRESHOLD_MS = 285000;
     37 
     38     private long mFrequentWifiScansIntervalSecs = 0;
     39     private int mNumFrequentWifiDisconnects = 0;
     40     private int mNumFrequentWifiAssociations = 0;
     41 
     42     private BugreportItem mBugreportItem = null;
     43 
     44     public WifiStatsRule (BugreportItem bugreportItem) {
     45         super(bugreportItem);
     46         mBugreportItem = bugreportItem;
     47     }
     48 
     49     @Override
     50     public void applyRule() {
     51         if (mBugreportItem.getDumpsys() == null || getTimeOnBattery() <= 0) {
     52             return;
     53         }
     54         DumpsysWifiStatsItem dumpsysWifiStatsItem = mBugreportItem.getDumpsys().getWifiStats();
     55         if (dumpsysWifiStatsItem == null) {
     56             return;
     57         }
     58         if (dumpsysWifiStatsItem.getNumWifiScans() > 0) {
     59             final long observedWifiScanIntervalMs = getTimeOnBattery() /
     60                     dumpsysWifiStatsItem.getNumWifiScans();
     61 
     62             if (observedWifiScanIntervalMs < WIFI_SCAN_INTERVAL_THRESHOLD_MS) {
     63                 mFrequentWifiScansIntervalSecs = TimeUnit.MILLISECONDS.toSeconds(
     64                         observedWifiScanIntervalMs);
     65             }
     66         }
     67         if (dumpsysWifiStatsItem.getNumWifiDisconnects() >= WIFI_DISCONNECT_THRESHOLD) {
     68             mNumFrequentWifiDisconnects = dumpsysWifiStatsItem.getNumWifiDisconnects();
     69         }
     70         if (dumpsysWifiStatsItem.getNumWifiAssociations() > WIFI_ASSOCIATION_THRESHOLD) {
     71             mNumFrequentWifiAssociations = dumpsysWifiStatsItem.getNumWifiAssociations();
     72         }
     73     }
     74 
     75     @Override
     76     public JSONObject getAnalysis() {
     77         JSONObject wifiStatsAnalysis = new JSONObject();
     78         StringBuilder analysis = new StringBuilder();
     79         if (mFrequentWifiScansIntervalSecs == 0) {
     80             analysis.append("No apps requested for frequent wifi scans. ");
     81         } else {
     82             analysis.append(String.format("Wifi scans happened every %d seconds. ",
     83                     mFrequentWifiScansIntervalSecs));
     84         }
     85         if (mNumFrequentWifiDisconnects == 0) {
     86             analysis.append("No frequent wifi disconnects were observed. ");
     87         } else {
     88             analysis.append(String.format("Wifi got disconnected %d times. ",
     89                     mNumFrequentWifiDisconnects));
     90         }
     91         if (mNumFrequentWifiAssociations == 0) {
     92             analysis.append("No frequent wifi associations were observed. ");
     93         } else {
     94             analysis.append(String.format("Wifi got associated with AP %d times. ",
     95                     mNumFrequentWifiAssociations));
     96         }
     97         try {
     98             wifiStatsAnalysis.put(WIFI_STATS, analysis.toString().trim());
     99         } catch (JSONException e) {
    100           // do nothing
    101         }
    102         return wifiStatsAnalysis;
    103     }
    104 }
    105