1package com.android.server.wifi.anqp;
2
3import java.net.ProtocolException;
4import java.nio.ByteBuffer;
5import java.nio.charset.StandardCharsets;
6import java.util.ArrayList;
7import java.util.Collections;
8import java.util.List;
9
10import static com.android.server.wifi.anqp.Constants.BYTE_MASK;
11
12/**
13 * The Network Authentication Type ANQP Element, IEEE802.11-2012 section 8.4.4.6
14 */
15public class NetworkAuthenticationTypeElement extends ANQPElement {
16
17    private final List<NetworkAuthentication> m_authenticationTypes;
18
19    public enum NwkAuthTypeEnum {
20        TermsAndConditions,
21        OnLineEnrollment,
22        HTTPRedirection,
23        DNSRedirection,
24        Reserved
25    }
26
27    public static class NetworkAuthentication {
28        private final NwkAuthTypeEnum m_type;
29        private final String m_url;
30
31        private NetworkAuthentication(NwkAuthTypeEnum type, String url) {
32            m_type = type;
33            m_url = url;
34        }
35
36        public NwkAuthTypeEnum getType() {
37            return m_type;
38        }
39
40        public String getURL() {
41            return m_url;
42        }
43
44        @Override
45        public String toString() {
46            return "NetworkAuthentication{" +
47                    "m_type=" + m_type +
48                    ", m_url='" + m_url + '\'' +
49                    '}';
50        }
51    }
52
53    public NetworkAuthenticationTypeElement(Constants.ANQPElementType infoID, ByteBuffer payload)
54            throws ProtocolException {
55
56        super(infoID);
57
58        m_authenticationTypes = new ArrayList<NetworkAuthentication>();
59
60        while (payload.hasRemaining()) {
61            int typeNumber = payload.get() & BYTE_MASK;
62            NwkAuthTypeEnum type;
63            type = typeNumber >= NwkAuthTypeEnum.values().length ?
64                    NwkAuthTypeEnum.Reserved :
65                    NwkAuthTypeEnum.values()[typeNumber];
66
67            m_authenticationTypes.add(new NetworkAuthentication(type,
68                    Constants.getPrefixedString(payload, 2, StandardCharsets.UTF_8)));
69        }
70    }
71
72    public List<NetworkAuthentication> getAuthenticationTypes() {
73        return Collections.unmodifiableList(m_authenticationTypes);
74    }
75
76    @Override
77    public String toString() {
78        return "NetworkAuthenticationType{" +
79                "m_authenticationTypes=" + m_authenticationTypes +
80                '}';
81    }
82}
83