ScanResultUtil.java revision 22b5eca14a99c2bbeeae8361c665923ce71e1603
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
17package com.android.server.wifi.util;
18
19import android.net.wifi.ScanResult;
20import android.net.wifi.WifiConfiguration;
21
22import com.android.server.wifi.ScanDetail;
23import com.android.server.wifi.WifiConfigurationUtil;
24import com.android.server.wifi.hotspot2.NetworkDetail;
25
26/**
27 * Scan result utility for any {@link ScanResult} related operations.
28 * Currently contains:
29 *   > Helper method for converting a ScanResult to a ScanDetail.
30 *     Only fields that are supported in ScanResult are copied.
31 *   > Helper methods to identify the encryption of a ScanResult.
32 */
33public class ScanResultUtil {
34    private ScanResultUtil() { /* not constructable */ }
35
36    /**
37     * This method should only be used when the informationElements field in the provided scan
38     * result is filled in with the IEs from the beacon.
39     */
40    public static ScanDetail toScanDetail(ScanResult scanResult) {
41        NetworkDetail networkDetail = new NetworkDetail(scanResult.BSSID,
42                scanResult.informationElements, scanResult.anqpLines, scanResult.frequency);
43        return new ScanDetail(scanResult, networkDetail, null);
44    }
45
46    /**
47     * Helper method to check if the provided |scanResult| corresponds to a PSK network or not.
48     * This checks if the provided capabilities string contains PSK encryption type or not.
49     */
50    public static boolean isScanResultForPskNetwork(ScanResult scanResult) {
51        return scanResult.capabilities.contains("PSK");
52    }
53
54    /**
55     * Helper method to check if the provided |scanResult| corresponds to a EAP network or not.
56     * This checks if the provided capabilities string contains EAP encryption type or not.
57     */
58    public static boolean isScanResultForEapNetwork(ScanResult scanResult) {
59        return scanResult.capabilities.contains("EAP");
60    }
61
62    /**
63     * Helper method to check if the provided |scanResult| corresponds to a WEP network or not.
64     * This checks if the provided capabilities string contains WEP encryption type or not.
65     */
66    public static boolean isScanResultForWepNetwork(ScanResult scanResult) {
67        return scanResult.capabilities.contains("WEP");
68    }
69
70    /**
71     * Helper method to check if the provided |scanResult| corresponds to an open network or not.
72     * This checks if the provided capabilities string does not contain either of WEP, PSK or EAP
73     * encryption types or not.
74     */
75    public static boolean isScanResultForOpenNetwork(ScanResult scanResult) {
76        return !(isScanResultForWepNetwork(scanResult) || isScanResultForPskNetwork(scanResult)
77                || isScanResultForEapNetwork(scanResult));
78    }
79
80    /**
81     * Helper method to check if the provided |scanResult| and |config| have the same
82     * encryption type.
83     */
84    public static boolean doesScanResultEncryptionMatchWithNetwork(
85            ScanResult scanResult, WifiConfiguration config) {
86        if (ScanResultUtil.isScanResultForPskNetwork(scanResult)
87                && WifiConfigurationUtil.isConfigForPskNetwork(config)) {
88            return true;
89        }
90        if (ScanResultUtil.isScanResultForEapNetwork(scanResult)
91                && WifiConfigurationUtil.isConfigForEapNetwork(config)) {
92            return true;
93        }
94        if (ScanResultUtil.isScanResultForWepNetwork(scanResult)
95                && WifiConfigurationUtil.isConfigForWepNetwork(config)) {
96            return true;
97        }
98        if (ScanResultUtil.isScanResultForOpenNetwork(scanResult)
99                && WifiConfigurationUtil.isConfigForOpenNetwork(config)) {
100            return true;
101        }
102        return false;
103    }
104
105}
106