1 package com.android.server.wifi.hotspot2.anqp;
2 
3 import java.net.ProtocolException;
4 import java.nio.ByteBuffer;
5 import java.nio.ByteOrder;
6 import java.nio.charset.Charset;
7 import java.util.Collection;
8 import java.util.EnumMap;
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12 
13 /**
14  * ANQP related constants (802.11-2012)
15  */
16 public class Constants {
17 
18     public static final int NIBBLE_MASK = 0x0f;
19     public static final int BYTE_MASK = 0xff;
20     public static final int SHORT_MASK = 0xffff;
21     public static final long INT_MASK = 0xffffffffL;
22     public static final int BYTES_IN_SHORT = 2;
23     public static final int BYTES_IN_INT = 4;
24     public static final int BYTES_IN_EUI48 = 6;
25     public static final long MILLIS_IN_A_SEC = 1000L;
26 
27     public static final int HS20_PREFIX = 0x119a6f50;   // Note that this is represented as a LE int
28     public static final int HS20_FRAME_PREFIX = 0x109a6f50;
29     public static final int UTF8_INDICATOR = 1;
30 
31     public static final int LANG_CODE_LENGTH = 3;
32     // From IEEE802.11-2012 section 8.4.1.34.
33     public static final int VENUE_INFO_LENGTH = 2;
34 
35     public static final int ANQP_QUERY_LIST = 256;
36     public static final int ANQP_VENUE_NAME = 258;
37     public static final int ANQP_ROAMING_CONSORTIUM = 261;
38     public static final int ANQP_IP_ADDR_AVAILABILITY = 262;
39     public static final int ANQP_NAI_REALM = 263;
40     public static final int ANQP_3GPP_NETWORK = 264;
41     public static final int ANQP_DOM_NAME = 268;
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_FRIENDLY_NAME = 3;
46     public static final int HS_WAN_METRICS = 4;
47     public static final int HS_CONN_CAPABILITY = 5;
48     public static final int HS_NAI_HOME_REALM_QUERY = 6;
49     public static final int HS_OSU_PROVIDERS = 8;
50     public static final int HS_ICON_REQUEST = 10;
51     public static final int HS_ICON_FILE = 11;
52 
53     public enum ANQPElementType {
54         ANQPQueryList,
55         ANQPVenueName,
56         ANQPRoamingConsortium,
57         ANQPIPAddrAvailability,
58         ANQPNAIRealm,
59         ANQP3GPPNetwork,
60         ANQPDomName,
61         ANQPVendorSpec,
62         HSQueryList,
63         HSFriendlyName,
64         HSWANMetrics,
65         HSConnCapability,
66         HSNAIHomeRealmQuery,
67         HSOSUProviders,
68         HSIconRequest,
69         HSIconFile
70     }
71 
72     private static final Map<Integer, ANQPElementType> sAnqpMap = new HashMap<>();
73     private static final Map<Integer, ANQPElementType> sHs20Map = new HashMap<>();
74     private static final Map<ANQPElementType, Integer> sRevAnqpmap =
75             new EnumMap<>(ANQPElementType.class);
76     private static final Map<ANQPElementType, Integer> sRevHs20map =
77             new EnumMap<>(ANQPElementType.class);
78 
79     static {
sAnqpMap.put(ANQP_QUERY_LIST, ANQPElementType.ANQPQueryList)80         sAnqpMap.put(ANQP_QUERY_LIST, ANQPElementType.ANQPQueryList);
sAnqpMap.put(ANQP_VENUE_NAME, ANQPElementType.ANQPVenueName)81         sAnqpMap.put(ANQP_VENUE_NAME, ANQPElementType.ANQPVenueName);
sAnqpMap.put(ANQP_ROAMING_CONSORTIUM, ANQPElementType.ANQPRoamingConsortium)82         sAnqpMap.put(ANQP_ROAMING_CONSORTIUM, ANQPElementType.ANQPRoamingConsortium);
sAnqpMap.put(ANQP_IP_ADDR_AVAILABILITY, ANQPElementType.ANQPIPAddrAvailability)83         sAnqpMap.put(ANQP_IP_ADDR_AVAILABILITY, ANQPElementType.ANQPIPAddrAvailability);
sAnqpMap.put(ANQP_NAI_REALM, ANQPElementType.ANQPNAIRealm)84         sAnqpMap.put(ANQP_NAI_REALM, ANQPElementType.ANQPNAIRealm);
sAnqpMap.put(ANQP_3GPP_NETWORK, ANQPElementType.ANQP3GPPNetwork)85         sAnqpMap.put(ANQP_3GPP_NETWORK, ANQPElementType.ANQP3GPPNetwork);
sAnqpMap.put(ANQP_DOM_NAME, ANQPElementType.ANQPDomName)86         sAnqpMap.put(ANQP_DOM_NAME, ANQPElementType.ANQPDomName);
sAnqpMap.put(ANQP_VENDOR_SPEC, ANQPElementType.ANQPVendorSpec)87         sAnqpMap.put(ANQP_VENDOR_SPEC, ANQPElementType.ANQPVendorSpec);
88 
sHs20Map.put(HS_QUERY_LIST, ANQPElementType.HSQueryList)89         sHs20Map.put(HS_QUERY_LIST, ANQPElementType.HSQueryList);
sHs20Map.put(HS_FRIENDLY_NAME, ANQPElementType.HSFriendlyName)90         sHs20Map.put(HS_FRIENDLY_NAME, ANQPElementType.HSFriendlyName);
sHs20Map.put(HS_WAN_METRICS, ANQPElementType.HSWANMetrics)91         sHs20Map.put(HS_WAN_METRICS, ANQPElementType.HSWANMetrics);
sHs20Map.put(HS_CONN_CAPABILITY, ANQPElementType.HSConnCapability)92         sHs20Map.put(HS_CONN_CAPABILITY, ANQPElementType.HSConnCapability);
sHs20Map.put(HS_NAI_HOME_REALM_QUERY, ANQPElementType.HSNAIHomeRealmQuery)93         sHs20Map.put(HS_NAI_HOME_REALM_QUERY, ANQPElementType.HSNAIHomeRealmQuery);
sHs20Map.put(HS_OSU_PROVIDERS, ANQPElementType.HSOSUProviders)94         sHs20Map.put(HS_OSU_PROVIDERS, ANQPElementType.HSOSUProviders);
sHs20Map.put(HS_ICON_REQUEST, ANQPElementType.HSIconRequest)95         sHs20Map.put(HS_ICON_REQUEST, ANQPElementType.HSIconRequest);
sHs20Map.put(HS_ICON_FILE, ANQPElementType.HSIconFile)96         sHs20Map.put(HS_ICON_FILE, ANQPElementType.HSIconFile);
97 
98         for (Map.Entry<Integer, ANQPElementType> entry : sAnqpMap.entrySet()) {
entry.getKey()99             sRevAnqpmap.put(entry.getValue(), entry.getKey());
100         }
101         for (Map.Entry<Integer, ANQPElementType> entry : sHs20Map.entrySet()) {
entry.getKey()102             sRevHs20map.put(entry.getValue(), entry.getKey());
103         }
104     }
105 
mapANQPElement(int id)106     public static ANQPElementType mapANQPElement(int id) {
107         return sAnqpMap.get(id);
108     }
109 
mapHS20Element(int id)110     public static ANQPElementType mapHS20Element(int id) {
111         return sHs20Map.get(id);
112     }
113 
getANQPElementID(ANQPElementType elementType)114     public static Integer getANQPElementID(ANQPElementType elementType) {
115         return sRevAnqpmap.get(elementType);
116     }
117 
getHS20ElementID(ANQPElementType elementType)118     public static Integer getHS20ElementID(ANQPElementType elementType) {
119         return sRevHs20map.get(elementType);
120     }
121 
hasBaseANQPElements(Collection<ANQPElementType> elements)122     public static boolean hasBaseANQPElements(Collection<ANQPElementType> elements) {
123         if (elements == null) {
124             return false;
125         }
126         for (ANQPElementType element : elements) {
127             if (sRevAnqpmap.containsKey(element)) {
128                 return true;
129             }
130         }
131         return false;
132     }
133 
hasR2Elements(List<ANQPElementType> elements)134     public static boolean hasR2Elements(List<ANQPElementType> elements) {
135         return elements.contains(ANQPElementType.HSOSUProviders);
136     }
137 }
138