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.io.PrintWriter;
9import java.util.ArrayList;
10import java.util.HashMap;
11import java.util.List;
12import java.util.Map;
13
14public class AnqpCache {
15    private static final long CACHE_RECHECK = 60000L;
16    private static final boolean STANDARD_ESS = true;  // Regular AP keying; see CacheKey below.
17    private long mLastSweep;
18
19    private final HashMap<CacheKey, ANQPData> mANQPCache;
20
21    public AnqpCache() {
22        mANQPCache = new HashMap<>();
23        mLastSweep = System.currentTimeMillis();
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        CacheKey key = CacheKey.buildKey(network, STANDARD_ESS);
120
121        synchronized (mANQPCache) {
122            ANQPData data = mANQPCache.get(key);
123            if (data == null || data.expired()) {
124                mANQPCache.put(key, new ANQPData(network, data));
125                return true;
126            }
127            else {
128                Log.d(Utils.hs2LogTag(getClass()),
129                      String.format("BSSID %012x already in cache: %s", network.getBSSID(), data));
130                return false;
131            }
132        }
133    }
134
135    public void update(NetworkDetail network,
136                       Map<Constants.ANQPElementType, ANQPElement> anqpElements) {
137
138        CacheKey key = CacheKey.buildKey(network, STANDARD_ESS);
139
140        // Networks with a 0 ANQP Domain ID are still cached, but with a very short expiry, just
141        // long enough to prevent excessive re-querying.
142        synchronized (mANQPCache) {
143            ANQPData data = new ANQPData(network, anqpElements);
144            mANQPCache.put(key, data);
145        }
146    }
147
148    public ANQPData getEntry(NetworkDetail network) {
149        ANQPData data;
150
151        CacheKey key = CacheKey.buildKey(network, STANDARD_ESS);
152        synchronized (mANQPCache) {
153            data = mANQPCache.get(key);
154        }
155
156        return data != null && data.isValid(network) ? data : null;
157    }
158
159    public void clear(boolean all, boolean debug) {
160        long now = System.currentTimeMillis();
161        synchronized (mANQPCache) {
162            if (all) {
163                mANQPCache.clear();
164                mLastSweep = now;
165            }
166            else if (now > mLastSweep + CACHE_RECHECK) {
167                List<CacheKey> retirees = new ArrayList<>();
168                for (Map.Entry<CacheKey, ANQPData> entry : mANQPCache.entrySet()) {
169                    if (entry.getValue().expired(now)) {
170                        retirees.add(entry.getKey());
171                    }
172                }
173                for (CacheKey key : retirees) {
174                    mANQPCache.remove(key);
175                    if (debug) {
176                        Log.d(Utils.hs2LogTag(getClass()), "Retired " + key);
177                    }
178                }
179                mLastSweep = now;
180            }
181        }
182    }
183
184    public void dump(PrintWriter out) {
185        out.println("Last sweep " + Utils.toHMS(System.currentTimeMillis() - mLastSweep) + " ago.");
186        for (ANQPData anqpData : mANQPCache.values()) {
187            out.println(anqpData.toString(false));
188        }
189    }
190}
191