ANQPData.java revision 82414ead13eb2554cf412ad00a30d9e21499bf75
1package com.android.server.wifi.hotspot2;
2
3import com.android.server.wifi.anqp.ANQPElement;
4import com.android.server.wifi.anqp.Constants;
5
6import java.util.Collections;
7import java.util.Map;
8
9public class ANQPData {
10    /**
11     * The regular cache time for entries with a non-zero domain id.
12     */
13    private static final long ANQP_QUALIFIED_CACHE_TIMEOUT = 3600000L;
14    /**
15     * The cache time for entries with a zero domain id. The zero domain id indicates that ANQP
16     * data from the AP may change at any time, thus a relatively short cache time is given to
17     * such data, but still long enough to avoid excessive querying.
18     */
19    private static final long ANQP_UNQUALIFIED_CACHE_TIMEOUT = 300000L;
20    /**
21     * This is the hold off time for pending queries, i.e. the time during which subsequent queries
22     * are squelched.
23     */
24    private static final long ANQP_HOLDOFF_TIME = 10000L;
25
26    /**
27     * Max value for the retry counter for unanswered queries. This limits the maximum time-out to
28     * ANQP_HOLDOFF_TIME * 2^MAX_RETRY. With current values this results in 640s.
29     */
30    private static final int MAX_RETRY = 6;
31
32    private final NetworkDetail mNetwork;
33    private final Map<Constants.ANQPElementType, ANQPElement> mANQPElements;
34    private final long mCtime;
35    private final long mExpiry;
36    private final int mRetry;
37
38    public ANQPData(NetworkDetail network,
39                    Map<Constants.ANQPElementType, ANQPElement> anqpElements) {
40
41        mNetwork = network;
42        mANQPElements = anqpElements != null ? Collections.unmodifiableMap(anqpElements) : null;
43        mCtime = System.currentTimeMillis();
44        mRetry = 0;
45        if (anqpElements == null) {
46            mExpiry = mCtime + ANQP_HOLDOFF_TIME;
47        }
48        else if (network.getAnqpDomainID() == 0) {
49            mExpiry = mCtime + ANQP_UNQUALIFIED_CACHE_TIMEOUT;
50        }
51        else {
52            mExpiry = mCtime + ANQP_QUALIFIED_CACHE_TIMEOUT;
53        }
54    }
55
56    public ANQPData(NetworkDetail network, ANQPData existing) {
57        mNetwork = network;
58        mANQPElements = null;
59        mCtime = System.currentTimeMillis();
60        if (existing == null) {
61            mRetry = 0;
62            mExpiry = mCtime + ANQP_HOLDOFF_TIME;
63        }
64        else {
65            mRetry = Math.max(existing.getRetry() + 1, MAX_RETRY);
66            mExpiry = ANQP_HOLDOFF_TIME * (1<<mRetry);
67        }
68    }
69
70    public Map<Constants.ANQPElementType, ANQPElement> getANQPElements() {
71        return Collections.unmodifiableMap(mANQPElements);
72    }
73
74    public NetworkDetail getNetwork() {
75        return mNetwork;
76    }
77
78    public boolean expired() {
79        return expired(System.currentTimeMillis());
80    }
81
82    public boolean expired(long at) {
83        return mExpiry <= at;
84    }
85
86    protected boolean isValid(NetworkDetail nwk) {
87        return mANQPElements != null &&
88                nwk.getAnqpDomainID() == mNetwork.getAnqpDomainID() &&
89                mExpiry > System.currentTimeMillis();
90    }
91
92    private int getRetry() {
93        return mRetry;
94    }
95
96    public String toString(boolean brief) {
97        StringBuilder sb = new StringBuilder();
98        sb.append(mNetwork.toKeyString()).append(", domid ").append(mNetwork.getAnqpDomainID());
99        if (mANQPElements == null) {
100            sb.append(", unresolved, ");
101        }
102        else {
103            sb.append(", ").append(mANQPElements.size()).append(" elements, ");
104        }
105        long now = System.currentTimeMillis();
106        sb.append(Utils.toHMS(now-mCtime)).append(" old, expires in ").
107                append(Utils.toHMS(mExpiry-now)).append(' ');
108        if (brief) {
109            sb.append(expired(now) ? 'x' : '-');
110            sb.append(mANQPElements == null ? 'u' : '-');
111        }
112        else if (mANQPElements != null) {
113            sb.append(" data=").append(mANQPElements);
114        }
115        return sb.toString();
116    }
117
118    @Override
119    public String toString() {
120        return toString(true);
121    }
122}
123