Home | History | Annotate | Download | only in parser
      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.loganalysis.parser;
     17 
     18 import com.android.loganalysis.item.DumpsysWifiStatsItem;
     19 
     20 import java.util.List;
     21 import java.util.regex.Matcher;
     22 import java.util.regex.Pattern;
     23 
     24 /**
     25  * A {@link IParser} to parse wifi stats and extract the number of disconnects and scans
     26  */
     27 public class DumpsysWifiStatsParser implements IParser {
     28 
     29     /**
     30      * Matches: 01-04 00:16:27.666 - Event [IFNAME=wlan0 CTRL-EVENT-SCAN-STARTED ]
     31      */
     32     private static final Pattern WIFI_SCAN = Pattern.compile(
     33             "^\\d+-\\d+ \\d+:\\d+:\\d+\\.\\d+ - Event \\[IFNAME=wlan0 CTRL-EVENT-SCAN-STARTED \\]");
     34 
     35     /**
     36      * Matches: 01-04 00:16:27.666 - Event [IFNAME=wlan0 CTRL-EVENT-DISCONNECTED
     37      * bssid=6c:f3:7f:ae:89:92 reason=0 locally_generated=1]
     38      */
     39     private static final Pattern WIFI_DISCONNECT = Pattern.compile(
     40             "^\\d+-\\d+ \\d+:\\d+:\\d+\\.\\d+ - Event \\[IFNAME=wlan0 CTRL-EVENT-DISCONNECTED "
     41             + "bssid=\\w+:\\w+:\\w+:\\w+:\\w+:\\w+ reason=\\d+(\\s*locally_generated=\\d+)?\\]");
     42 
     43     /**
     44      * Matches: 01-21 18:17:23.15 - Event [IFNAME=wlan0 Trying to associate with SSID 'WL-power-2']
     45      */
     46     private static final Pattern WIFI_ASSOCIATION = Pattern.compile(
     47             "^\\d+-\\d+ \\d+:\\d+:\\d+\\.\\d+ - Event \\[IFNAME=wlan0 Trying to associate with "
     48             + "SSID \\'.*\\'\\]");
     49 
     50     /**
     51      * {@inheritDoc}
     52      *
     53      * @return The {@link DumpsysWifiStatsItem}.
     54      */
     55     @Override
     56     public DumpsysWifiStatsItem parse(List<String> lines) {
     57         DumpsysWifiStatsItem item = new DumpsysWifiStatsItem();
     58         int numWifiScans = 0;
     59         int numWifiDisconnects = 0;
     60         int numWifiAssociations = 0;
     61         for (String line : lines) {
     62             Matcher m = WIFI_SCAN.matcher(line);
     63             if(m.matches()) {
     64                 numWifiScans++;
     65                 continue;
     66             }
     67             m = WIFI_DISCONNECT.matcher(line);
     68             if (m.matches()) {
     69                 numWifiDisconnects++;
     70                 continue;
     71             }
     72             m = WIFI_ASSOCIATION.matcher(line);
     73             if (m.matches()) {
     74                 numWifiAssociations++;
     75             }
     76         }
     77         item.setNumWifiScan(numWifiScans);
     78         item.setNumWifiDisconnect(numWifiDisconnects);
     79         item.setNumWifiAssociation(numWifiAssociations);
     80         return item;
     81     }
     82 
     83 }
     84