1 /* 2 * Copyright (C) 2017 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.net.wifi.ScanResult; 19 import android.net.wifi.WifiConfiguration; 20 21 import com.android.server.wifi.util.ScanResultUtil; 22 23 import java.util.Objects; 24 25 /** 26 * Class to store the info needed to match a scan result to the provided network configuration. 27 */ 28 public class ScanResultMatchInfo { 29 public static final int NETWORK_TYPE_OPEN = 0; 30 public static final int NETWORK_TYPE_WEP = 1; 31 public static final int NETWORK_TYPE_PSK = 2; 32 public static final int NETWORK_TYPE_EAP = 3; 33 34 /** 35 * SSID of the network. 36 */ 37 public String networkSsid; 38 /** 39 * Security Type of the network. 40 */ 41 public int networkType; 42 43 /** 44 * Get the ScanResultMatchInfo for the given WifiConfiguration 45 */ 46 public static ScanResultMatchInfo fromWifiConfiguration(WifiConfiguration config) { 47 ScanResultMatchInfo info = new ScanResultMatchInfo(); 48 info.networkSsid = config.SSID; 49 if (WifiConfigurationUtil.isConfigForPskNetwork(config)) { 50 info.networkType = NETWORK_TYPE_PSK; 51 } else if (WifiConfigurationUtil.isConfigForEapNetwork(config)) { 52 info.networkType = NETWORK_TYPE_EAP; 53 } else if (WifiConfigurationUtil.isConfigForWepNetwork(config)) { 54 info.networkType = NETWORK_TYPE_WEP; 55 } else if (WifiConfigurationUtil.isConfigForOpenNetwork(config)) { 56 info.networkType = NETWORK_TYPE_OPEN; 57 } else { 58 throw new IllegalArgumentException("Invalid WifiConfiguration: " + config); 59 } 60 return info; 61 } 62 63 /** 64 * Get the ScanResultMatchInfo for the given ScanResult 65 */ 66 public static ScanResultMatchInfo fromScanResult(ScanResult scanResult) { 67 ScanResultMatchInfo info = new ScanResultMatchInfo(); 68 // Scan result ssid's are not quoted, hence add quotes. 69 // TODO: This matching algo works only if the scan result contains a string SSID. 70 // However, according to our public documentation ths {@link WifiConfiguration#SSID} can 71 // either have a hex string or quoted ASCII string SSID. 72 info.networkSsid = ScanResultUtil.createQuotedSSID(scanResult.SSID); 73 if (ScanResultUtil.isScanResultForPskNetwork(scanResult)) { 74 info.networkType = NETWORK_TYPE_PSK; 75 } else if (ScanResultUtil.isScanResultForEapNetwork(scanResult)) { 76 info.networkType = NETWORK_TYPE_EAP; 77 } else if (ScanResultUtil.isScanResultForWepNetwork(scanResult)) { 78 info.networkType = NETWORK_TYPE_WEP; 79 } else if (ScanResultUtil.isScanResultForOpenNetwork(scanResult)) { 80 info.networkType = NETWORK_TYPE_OPEN; 81 } else { 82 throw new IllegalArgumentException("Invalid ScanResult: " + scanResult); 83 } 84 return info; 85 } 86 87 @Override 88 public boolean equals(Object otherObj) { 89 if (this == otherObj) { 90 return true; 91 } else if (!(otherObj instanceof ScanResultMatchInfo)) { 92 return false; 93 } 94 ScanResultMatchInfo other = (ScanResultMatchInfo) otherObj; 95 return Objects.equals(networkSsid, other.networkSsid) 96 && networkType == other.networkType; 97 } 98 99 @Override 100 public int hashCode() { 101 return Objects.hash(networkSsid, networkType); 102 } 103 104 @Override 105 public String toString() { 106 return "ScanResultMatchInfo: " + networkSsid + ", type: " + networkType; 107 } 108 } 109