1package com.android.server.wifi.anqp;
2
3import java.net.ProtocolException;
4import java.nio.ByteBuffer;
5import java.util.ArrayList;
6import java.util.Collections;
7import java.util.List;
8
9/**
10 * The Connection Capability vendor specific ANQP Element,
11 * Wi-Fi Alliance Hotspot 2.0 (Release 2) Technical Specification - Version 5.00,
12 * section 4.5
13 */
14public class HSConnectionCapabilityElement extends ANQPElement {
15
16    public enum ProtoStatus {Closed, Open, Unknown}
17
18    private final List<ProtocolTuple> mStatusList;
19
20    public static class ProtocolTuple {
21        private final int mProtocol;
22        private final int mPort;
23        private final ProtoStatus mStatus;
24
25        private ProtocolTuple(ByteBuffer payload) throws ProtocolException {
26            if (payload.remaining() < 4) {
27                throw new ProtocolException("Runt protocol tuple: " + payload.remaining());
28            }
29            mProtocol = payload.get() & Constants.BYTE_MASK;
30            mPort = payload.getShort() & Constants.SHORT_MASK;
31            int statusNumber = payload.get() & Constants.BYTE_MASK;
32            mStatus = statusNumber < ProtoStatus.values().length ?
33                    ProtoStatus.values()[statusNumber] :
34                    null;
35        }
36
37        public int getProtocol() {
38            return mProtocol;
39        }
40
41        public int getPort() {
42            return mPort;
43        }
44
45        public ProtoStatus getStatus() {
46            return mStatus;
47        }
48
49        @Override
50        public String toString() {
51            return "ProtocolTuple{" +
52                    "mProtocol=" + mProtocol +
53                    ", mPort=" + mPort +
54                    ", mStatus=" + mStatus +
55                    '}';
56        }
57    }
58
59    public HSConnectionCapabilityElement(Constants.ANQPElementType infoID, ByteBuffer payload)
60            throws ProtocolException {
61        super(infoID);
62
63        mStatusList = new ArrayList<>();
64        while (payload.hasRemaining()) {
65            mStatusList.add(new ProtocolTuple(payload));
66        }
67    }
68
69    public List<ProtocolTuple> getStatusList() {
70        return Collections.unmodifiableList(mStatusList);
71    }
72
73    @Override
74    public String toString() {
75        return "HSConnectionCapability{" +
76                "mStatusList=" + mStatusList +
77                '}';
78    }
79}
80