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.server.wifi.hotspot2;
18 
19 import android.text.TextUtils;
20 
21 /**
22  * Unique key for identifying APs that will contain the same ANQP information.
23  *
24  * APs in the same ESS (SSID or HESSID) with the same ANQP domain ID will have the same ANQP
25  * information. Thus, those APs will be keyed by the ESS identifier (SSID or HESSID) and the
26  * ANQP domain ID.
27  *
28  * APs without ANQP domain ID set will assumed to have unique ANQP information. Thus, those
29  * APs will be keyed by SSID and BSSID.
30  */
31 public class ANQPNetworkKey {
32     private final String mSSID;
33     private final long mBSSID;
34     private final long mHESSID;
35     private final int mAnqpDomainID;
36 
ANQPNetworkKey(String ssid, long bssid, long hessid, int anqpDomainID)37     public ANQPNetworkKey(String ssid, long bssid, long hessid, int anqpDomainID) {
38         mSSID = ssid;
39         mBSSID = bssid;
40         mHESSID = hessid;
41         mAnqpDomainID = anqpDomainID;
42     }
43 
44     /**
45      * Build an ANQP network key suitable for the granularity of the key space as follows:
46      *
47      * HESSID   domainID    Key content       Rationale
48      * -------- ----------- -----------       --------------------
49      * n/a      zero        SSID/BSSID        Domain ID indicates unique AP info
50      * not set  set         SSID/domainID     Standard definition of an ESS
51      * set      set         HESSID/domainID   The ESS is defined by the HESSID
52      *
53      * @param ssid The SSID of the AP
54      * @param bssid The BSSID of the AP
55      * @param hessid The HESSID of the AP
56      * @param anqpDomainId The ANQP Domain ID of the AP
57      * @return {@link ANQPNetworkKey}
58      */
buildKey(String ssid, long bssid, long hessid, int anqpDomainId)59     public static ANQPNetworkKey buildKey(String ssid, long bssid, long hessid, int anqpDomainId) {
60         if (anqpDomainId == 0) {
61             return new ANQPNetworkKey(ssid, bssid, 0, 0);
62         } else if (hessid != 0L) {
63             return new ANQPNetworkKey(null, 0, hessid, anqpDomainId);
64         }
65         return new ANQPNetworkKey(ssid, 0, 0, anqpDomainId);
66     }
67 
68     @Override
hashCode()69     public int hashCode() {
70         if (mHESSID != 0) {
71             return (int) (((mHESSID >>> 32) * 31 + mHESSID) * 31 + mAnqpDomainID);
72         } else if (mBSSID != 0) {
73             return (int) ((mSSID.hashCode() * 31 + (mBSSID >>> 32)) * 31 + mBSSID);
74         } else {
75             return mSSID.hashCode() * 31 + mAnqpDomainID;
76         }
77     }
78 
79     @Override
equals(Object thatObject)80     public boolean equals(Object thatObject) {
81         if (thatObject == this) {
82             return true;
83         }
84         if (!(thatObject instanceof ANQPNetworkKey)) {
85             return false;
86         }
87         ANQPNetworkKey that = (ANQPNetworkKey) thatObject;
88         return TextUtils.equals(that.mSSID, mSSID)
89                 && that.mBSSID == mBSSID
90                 && that.mHESSID == mHESSID
91                 && that.mAnqpDomainID == mAnqpDomainID;
92     }
93 
94     @Override
toString()95     public String toString() {
96         if (mHESSID != 0L) {
97             return Utils.macToString(mHESSID) + ":" + mAnqpDomainID;
98         } else if (mBSSID != 0L) {
99             return Utils.macToString(mBSSID) + ":<" + mSSID + ">";
100         } else {
101             return "<" + mSSID + ">:" + mAnqpDomainID;
102         }
103     }
104 }
105