ScanScheduleUtil.java revision 4e54617758f86acef751bc8588257a58ed985b0f
1/* 2 * Copyright (C) 2015 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 17package com.android.server.wifi.scanner; 18 19import android.annotation.Nullable; 20import android.net.wifi.ScanResult; 21import android.net.wifi.WifiScanner.ScanData; 22import android.net.wifi.WifiScanner.ScanSettings; 23 24import com.android.server.wifi.WifiNative; 25 26import java.util.ArrayList; 27import java.util.List; 28 29/** 30 * A class with utilities for dealing with scan schedules. 31 */ 32public class ScanScheduleUtil { 33 34 /** 35 * Compares two ChannelSettings for equality. 36 */ 37 public static boolean channelEquals(@Nullable WifiNative.ChannelSettings channel1, 38 @Nullable WifiNative.ChannelSettings channel2) { 39 if (channel1 == null || channel2 == null) return false; 40 if (channel1 == channel2) return true; 41 42 if (channel1.frequency != channel2.frequency) return false; 43 if (channel1.dwell_time_ms != channel2.dwell_time_ms) return false; 44 return channel1.passive == channel2.passive; 45 } 46 47 /** 48 * Compares two BucketSettings for equality. 49 */ 50 public static boolean bucketEquals(@Nullable WifiNative.BucketSettings bucket1, 51 @Nullable WifiNative.BucketSettings bucket2) { 52 if (bucket1 == null || bucket2 == null) return false; 53 if (bucket1 == bucket2) return true; 54 55 if (bucket1.bucket != bucket2.bucket) return false; 56 if (bucket1.band != bucket2.band) return false; 57 if (bucket1.period_ms != bucket2.period_ms) return false; 58 if (bucket1.report_events != bucket2.report_events) return false; 59 if (bucket1.num_channels != bucket2.num_channels) return false; 60 for (int c = 0; c < bucket1.num_channels; c++) { 61 if (!channelEquals(bucket1.channels[c], bucket2.channels[c])) { 62 return false; 63 } 64 } 65 66 return true; 67 } 68 69 /** 70 * Compares two ScanSettings for equality. 71 */ 72 public static boolean scheduleEquals(@Nullable WifiNative.ScanSettings schedule1, 73 @Nullable WifiNative.ScanSettings schedule2) { 74 if (schedule1 == null || schedule2 == null) return false; 75 if (schedule1 == schedule2) return true; 76 77 if (schedule1.base_period_ms != schedule2.base_period_ms) return false; 78 if (schedule1.max_ap_per_scan != schedule2.max_ap_per_scan) return false; 79 if (schedule1.report_threshold_percent != schedule2.report_threshold_percent) return false; 80 if (schedule1.report_threshold_num_scans != schedule2.report_threshold_num_scans) { 81 return false; 82 } 83 if (schedule1.num_buckets != schedule2.num_buckets) return false; 84 for (int b = 0; b < schedule1.num_buckets; b++) { 85 if (!bucketEquals(schedule1.buckets[b], schedule2.buckets[b])) { 86 return false; 87 } 88 } 89 90 return true; 91 } 92 93 /** 94 * Returns true if the given scan result should be reported to a listener with the given 95 * settings. 96 */ 97 public static boolean shouldReportFullScanResultForSettings(ChannelHelper channelHelper, 98 ScanResult result, ScanSettings settings) { 99 return channelHelper.settingsContainChannel(settings, result.frequency); 100 } 101 102 /** 103 * Returns a filtered version of the scan results from the chip that represents only the data 104 * requested in the settings. Will return null if the result should not be reported. 105 */ 106 public static ScanData[] filterResultsForSettings(ChannelHelper channelHelper, 107 ScanData[] scanDatas, ScanSettings settings) { 108 List<ScanData> filteredScanDatas = new ArrayList<>(scanDatas.length); 109 List<ScanResult> filteredResults = new ArrayList<>(); 110 for (ScanData scanData : scanDatas) { 111 filteredResults.clear(); 112 for (ScanResult scanResult : scanData.getResults()) { 113 if (channelHelper.settingsContainChannel(settings, scanResult.frequency)) { 114 filteredResults.add(scanResult); 115 } 116 if (settings.numBssidsPerScan > 0 117 && filteredResults.size() >= settings.numBssidsPerScan) { 118 break; 119 } 120 } 121 if (filteredResults.size() == scanData.getResults().length) { 122 filteredScanDatas.add(scanData); 123 } else if (filteredResults.size() > 0) { 124 filteredScanDatas.add(new ScanData(scanData.getId(), 125 scanData.getFlags(), 126 filteredResults.toArray( 127 new ScanResult[filteredResults.size()]))); 128 } 129 } 130 if (filteredScanDatas.size() == 0) { 131 return null; 132 } else { 133 return filteredScanDatas.toArray(new ScanData[filteredScanDatas.size()]); 134 } 135 } 136} 137