Constants.java revision 777f4fc037d3ebd555f10041f8acfb41515b2f4b
1package com.android.server.wifi.anqp;
2
3import java.net.ProtocolException;
4import java.nio.ByteBuffer;
5import java.nio.charset.Charset;
6import java.util.HashMap;
7import java.util.Map;
8
9/**
10 * ANQP related constants (802.11-2012)
11 */
12public class Constants {
13
14    public static final int BYTE_MASK = 0xff;
15    public static final int SHORT_MASK = 0xffff;
16    public static final long INT_MASK = 0xffffffffL;
17    public static final int BYTES_IN_SHORT = 2;
18    public static final int BYTES_IN_INT = 4;
19
20    public static final int HS20_PREFIX = 0x119a6f50;   // Note that is represented as a LE int
21    public static final int UTF8_INDICATOR = 1;
22
23    public enum IconStatus {Success, FileNotFound, Unspecified}
24
25    public static final int ANQP_QUERY_LIST = 256;
26    public static final int ANQP_CAPABILITY_LIST = 257;
27    public static final int ANQP_VENUE_NAME = 258;
28    public static final int ANQP_EMERGENCY_NUMBER = 259;
29    public static final int ANQP_NWK_AUTH_TYPE = 260;
30    public static final int ANQP_ROAMING_CONSORTIUM = 261;
31    public static final int ANQP_IP_ADDR_AVAILABILITY = 262;
32    public static final int ANQP_NAI_REALM = 263;
33    public static final int ANQP_3GPP_NETWORK = 264;
34    public static final int ANQP_GEO_LOC = 265;
35    public static final int ANQP_CIVIC_LOC = 266;
36    public static final int ANQP_LOC_URI = 267;
37    public static final int ANQP_DOM_NAME = 268;
38    public static final int ANQP_EMERGENCY_ALERT = 269;
39    public static final int ANQP_TDLS_CAP = 270;
40    public static final int ANQP_EMERGENCY_NAI = 271;
41    public static final int ANQP_NEIGHBOR_REPORT = 272;
42    public static final int ANQP_VENDOR_SPEC = 56797;
43
44    public static final int HS_QUERY_LIST = 1;
45    public static final int HS_CAPABILITY_LIST = 2;
46    public static final int HS_FRIENDLY_NAME = 3;
47    public static final int HS_WAN_METRICS = 4;
48    public static final int HS_CONN_CAPABILITY = 5;
49    public static final int HS_NAI_HOME_REALM_QUERY = 6;
50    public static final int HS_OPERATING_CLASS = 7;
51    public static final int HS_OSU_PROVIDERS = 8;
52    public static final int HS_ICON_REQUEST = 10;
53    public static final int HS_ICON_FILE = 11;
54
55    public enum ANQPElementType {
56        ANQPQueryList,
57        ANQPCapabilityList,
58        ANQPVenueName,
59        ANQPEmergencyNumber,
60        ANQPNwkAuthType,
61        ANQPRoamingConsortium,
62        ANQPIPAddrAvailability,
63        ANQPNAIRealm,
64        ANQP3GPPNetwork,
65        ANQPGeoLoc,
66        ANQPCivicLoc,
67        ANQPLocURI,
68        ANQPDomName,
69        ANQPEmergencyAlert,
70        ANQPTDLSCap,
71        ANQPEmergencyNAI,
72        ANQPNeighborReport,
73        ANQPVendorSpec,
74        HSQueryList,
75        HSCapabilityList,
76        HSFriendlyName,
77        HSWANMetrics,
78        HSConnCapability,
79        HSNAIHomeRealmQuery,
80        HSOperatingclass,
81        HSOSUProviders,
82        HSIconRequest,
83        HSIconFile
84    }
85
86    private static final Map<Integer, ANQPElementType> sAnqpMap = new HashMap<Integer, ANQPElementType>();
87    private static final Map<Integer, ANQPElementType> sHs20Map = new HashMap<Integer, ANQPElementType>();
88    private static final Map<ANQPElementType, Integer> sRevAnqpmap = new HashMap<ANQPElementType, Integer>();
89    private static final Map<ANQPElementType, Integer> sRevHs20map = new HashMap<ANQPElementType, Integer>();
90
91    static {
92        sAnqpMap.put(ANQP_QUERY_LIST, ANQPElementType.ANQPQueryList);
93        sAnqpMap.put(ANQP_CAPABILITY_LIST, ANQPElementType.ANQPCapabilityList);
94        sAnqpMap.put(ANQP_VENUE_NAME, ANQPElementType.ANQPVenueName);
95        sAnqpMap.put(ANQP_EMERGENCY_NUMBER, ANQPElementType.ANQPEmergencyNumber);
96        sAnqpMap.put(ANQP_NWK_AUTH_TYPE, ANQPElementType.ANQPNwkAuthType);
97        sAnqpMap.put(ANQP_ROAMING_CONSORTIUM, ANQPElementType.ANQPRoamingConsortium);
98        sAnqpMap.put(ANQP_IP_ADDR_AVAILABILITY, ANQPElementType.ANQPIPAddrAvailability);
99        sAnqpMap.put(ANQP_NAI_REALM, ANQPElementType.ANQPNAIRealm);
100        sAnqpMap.put(ANQP_3GPP_NETWORK, ANQPElementType.ANQP3GPPNetwork);
101        sAnqpMap.put(ANQP_GEO_LOC, ANQPElementType.ANQPGeoLoc);
102        sAnqpMap.put(ANQP_CIVIC_LOC, ANQPElementType.ANQPCivicLoc);
103        sAnqpMap.put(ANQP_LOC_URI, ANQPElementType.ANQPLocURI);
104        sAnqpMap.put(ANQP_DOM_NAME, ANQPElementType.ANQPDomName);
105        sAnqpMap.put(ANQP_EMERGENCY_ALERT, ANQPElementType.ANQPEmergencyAlert);
106        sAnqpMap.put(ANQP_TDLS_CAP, ANQPElementType.ANQPTDLSCap);
107        sAnqpMap.put(ANQP_EMERGENCY_NAI, ANQPElementType.ANQPEmergencyNAI);
108        sAnqpMap.put(ANQP_NEIGHBOR_REPORT, ANQPElementType.ANQPNeighborReport);
109        sAnqpMap.put(ANQP_VENDOR_SPEC, ANQPElementType.ANQPVendorSpec);
110
111        sHs20Map.put(HS_QUERY_LIST, ANQPElementType.HSQueryList);
112        sHs20Map.put(HS_CAPABILITY_LIST, ANQPElementType.HSCapabilityList);
113        sHs20Map.put(HS_FRIENDLY_NAME, ANQPElementType.HSFriendlyName);
114        sHs20Map.put(HS_WAN_METRICS, ANQPElementType.HSWANMetrics);
115        sHs20Map.put(HS_CONN_CAPABILITY, ANQPElementType.HSConnCapability);
116        sHs20Map.put(HS_NAI_HOME_REALM_QUERY, ANQPElementType.HSNAIHomeRealmQuery);
117        sHs20Map.put(HS_OPERATING_CLASS, ANQPElementType.HSOperatingclass);
118        sHs20Map.put(HS_OSU_PROVIDERS, ANQPElementType.HSOSUProviders);
119        sHs20Map.put(HS_ICON_REQUEST, ANQPElementType.HSIconRequest);
120        sHs20Map.put(HS_ICON_FILE, ANQPElementType.HSIconFile);
121
122        for (Map.Entry<Integer, ANQPElementType> entry : sAnqpMap.entrySet()) {
123            sRevAnqpmap.put(entry.getValue(), entry.getKey());
124        }
125        for (Map.Entry<Integer, ANQPElementType> entry : sHs20Map.entrySet()) {
126            sRevHs20map.put(entry.getValue(), entry.getKey());
127        }
128    }
129
130    public static ANQPElementType mapANQPElement(int id) {
131        return sAnqpMap.get(id);
132    }
133
134    public static ANQPElementType mapHS20Element(int id) {
135        return sHs20Map.get(id);
136    }
137
138    public static Integer getANQPElementID(ANQPElementType elementType) {
139        return sRevAnqpmap.get(elementType);
140    }
141
142    public static Integer getHS20ElementID(ANQPElementType elementType) {
143        return sRevHs20map.get(elementType);
144    }
145
146    public static long getInteger(ByteBuffer payload, int size) {
147        byte[] octets = new byte[size];
148        payload.get(octets);
149        long value = 0;
150        for (int n = octets.length - 1; n >= 0; n--) {
151            value = (value << Byte.SIZE) | (octets[n] & BYTE_MASK);
152        }
153        return value;
154    }
155
156    public static String getPrefixedString(ByteBuffer payload, int lengthLength, Charset charset)
157            throws ProtocolException {
158        return getPrefixedString(payload, lengthLength, charset, false);
159    }
160
161    public static String getPrefixedString(ByteBuffer payload, int lengthLength, Charset charset,
162                                           boolean useNull) throws ProtocolException {
163        if (payload.remaining() < lengthLength) {
164            throw new ProtocolException("Runt string: " + payload.remaining());
165        }
166        return getString(payload, (int) getInteger(payload, lengthLength), charset, useNull);
167    }
168
169    public static String getString(ByteBuffer payload, int length, Charset charset)
170            throws ProtocolException {
171        return getString(payload, length, charset, false);
172    }
173
174    public static String getString(ByteBuffer payload, int length, Charset charset, boolean useNull)
175            throws ProtocolException {
176        if (length > payload.remaining()) {
177            throw new ProtocolException("Bad string length: " + length);
178        }
179        if (useNull && length == 0) {
180            return null;
181        }
182        byte[] octets = new byte[length];
183        payload.get(octets);
184        return new String(octets, charset);
185    }
186
187    public static String toHexString(byte[] data) {
188        StringBuilder sb = new StringBuilder(data.length * 3);
189
190        boolean first = true;
191        for (byte b : data) {
192            if (first) {
193                first = false;
194            } else {
195                sb.append(' ');
196            }
197            sb.append(String.format("%02x", b & BYTE_MASK));
198        }
199        return sb.toString();
200    }
201}
202