AnqpCache.java revision 8366a82ee1bff4c66d3f3642bf0a60538de5d9da
1package com.android.server.wifi.hotspot2;
2
3import android.util.Log;
4
5import com.android.server.wifi.anqp.ANQPElement;
6import com.android.server.wifi.anqp.Constants;
7
8import java.util.ArrayList;
9import java.util.HashMap;
10import java.util.List;
11import java.util.Map;
12
13public class AnqpCache implements AlarmHandler {
14    private static final long CACHE_RECHECK = 60000L;
15    private static final boolean STANDARD_ESS = true;  // Regular AP keying; see CacheKey below.
16
17    private final HashMap<CacheKey, ANQPData> mANQPCache;
18    private final Chronograph mChronograph;
19
20    public AnqpCache(Chronograph chronograph) {
21        mANQPCache = new HashMap<>();
22        mChronograph = chronograph;
23        mChronograph.addAlarm(CACHE_RECHECK, this, null);
24    }
25
26    private static class CacheKey {
27        private final String mSSID;
28        private final long mBSSID;
29        private final long mHESSID;
30
31        private CacheKey(String ssid, long bssid, long hessid) {
32            mSSID = ssid;
33            mBSSID = bssid;
34            mHESSID = hessid;
35        }
36
37        /**
38         * Build an ANQP cache key suitable for the granularity of the key space as follows:
39         *
40         * HESSID   domainID    standardESS     Key content Rationale
41         * -------- ----------- --------------- ----------- --------------------
42         * n/a      zero        n/a             SSID/BSSID  Domain ID indicates unique AP info
43         * not set  set         false           SSID/BSSID  Strict per AP keying override
44         * not set  set         true            SSID        Standard definition of an ESS
45         * set      set         n/a             HESSID      The ESS is defined by the HESSID
46         *
47         * @param network The network to build the key for.
48         * @param standardESS If this parameter is set the "standard" paradigm for an ESS is used
49         *                    for the cache, i.e. all APs with identical SSID is considered an ESS,
50         *                    otherwise caching is performed per AP.
51         * @return A CacheKey.
52         */
53        private static CacheKey buildKey(NetworkDetail network, boolean standardESS) {
54            String ssid;
55            long bssid;
56            long hessid;
57            if (network.getAnqpDomainID() == 0L || (network.getHESSID() == 0L && !standardESS)) {
58                ssid = network.getSSID();
59                bssid = network.getBSSID();
60                hessid = 0L;
61            }
62            else if (network.getHESSID() != 0L && network.getAnqpDomainID() > 0) {
63                ssid = null;
64                bssid = 0L;
65                hessid = network.getHESSID();
66            }
67            else {
68                ssid = network.getSSID();
69                bssid = 0L;
70                hessid = 0L;
71            }
72
73            return new CacheKey(ssid, bssid, hessid);
74        }
75
76        @Override
77        public int hashCode() {
78            if (mHESSID != 0) {
79                return (int)((mHESSID >>> 32) * 31 + mHESSID);
80            }
81            else if (mBSSID != 0) {
82                return (int)((mSSID.hashCode() * 31 + (mBSSID >>> 32)) * 31 + mBSSID);
83            }
84            else {
85                return mSSID.hashCode();
86            }
87        }
88
89        @Override
90        public boolean equals(Object thatObject) {
91            if (thatObject == this) {
92                return true;
93            }
94            else if (thatObject == null || thatObject.getClass() != CacheKey.class) {
95                return false;
96            }
97            CacheKey that = (CacheKey) thatObject;
98            return Utils.compare(that.mSSID, mSSID) == 0 &&
99                    that.mBSSID == mBSSID &&
100                    that.mHESSID == mHESSID;
101        }
102
103        @Override
104        public String toString() {
105            if (mHESSID != 0L) {
106                return "HESSID:" + NetworkDetail.toMACString(mHESSID);
107            }
108            else if (mBSSID != 0L) {
109                return NetworkDetail.toMACString(mBSSID) +
110                        ":<" + Utils.toUnicodeEscapedString(mSSID) + ">";
111            }
112            else {
113                return '<' + Utils.toUnicodeEscapedString(mSSID) + '>';
114            }
115        }
116    }
117
118    public boolean initiate(NetworkDetail network) {
119
120        long now = System.currentTimeMillis();
121        CacheKey key = CacheKey.buildKey(network, STANDARD_ESS);
122
123        synchronized (mANQPCache) {
124            ANQPData data = mANQPCache.get(key);
125            if (data == null ||
126                    (data.isResolved() && data.getDomainID() != network.getAnqpDomainID()) ||
127                    data.expendable(now)) {
128                mANQPCache.put(key, new ANQPData(network, null));
129                return true;
130            }
131            else {
132                return false;
133            }
134        }
135    }
136
137    public void update(NetworkDetail network,
138                       Map<Constants.ANQPElementType, ANQPElement> anqpElements) {
139
140        long now = System.currentTimeMillis();
141        CacheKey key = CacheKey.buildKey(network, STANDARD_ESS);
142
143        // Networks with a 0 ANQP Domain ID are still cached, but with a very short expiry, just
144        // long enough to prevent excessive re-querying.
145        synchronized (mANQPCache) {
146            ANQPData data = mANQPCache.get(key);
147            if (data == null ||
148                    !data.isResolved() ||
149                    data.getDomainID() != network.getAnqpDomainID() ||
150                    data.recacheable(now)) {
151                Log.d("HS2J", "Updating " + key + " -> " + data);
152                data = new ANQPData(network, anqpElements);
153                mANQPCache.put(key, data);
154            }
155            else {
156                Log.d("HS2J", "Not recaching " + key + " -> " + data);
157            }
158        }
159    }
160
161    public ANQPData getEntry(NetworkDetail network) {
162        ANQPData data;
163
164        CacheKey key = CacheKey.buildKey(network, STANDARD_ESS);
165        synchronized (mANQPCache) {
166            data = mANQPCache.get(key);
167        }
168
169        long now = System.currentTimeMillis();
170
171        if (data == null ||
172                !data.isResolved() ||
173                data.getDomainID() != network.getAnqpDomainID() ||
174                data.expired(now)) {
175            return null;
176        }
177        return data;
178    }
179
180    public void clear() {
181        synchronized (mANQPCache) {
182            mANQPCache.clear();
183        }
184    }
185
186    @Override
187    public void wake(Object token) {
188        long now = System.currentTimeMillis();
189        synchronized (mANQPCache) {
190            List<CacheKey> regulars = new ArrayList<>();
191            for (Map.Entry<CacheKey, ANQPData> entry : mANQPCache.entrySet()) {
192                if (entry.getValue().expired(now)) {
193                    regulars.add(entry.getKey());
194                }
195            }
196            for (CacheKey key : regulars) {
197                mANQPCache.remove(key);
198                Log.d("HS2J", "Retired " + key);
199            }
200        }
201        mChronograph.addAlarm(CACHE_RECHECK, this, null);
202    }
203}
204