1package com.android.server.wifi.anqp;
2
3import java.net.ProtocolException;
4import java.nio.ByteBuffer;
5import java.nio.charset.StandardCharsets;
6
7import static com.android.server.wifi.anqp.Constants.BYTE_MASK;
8import static com.android.server.wifi.anqp.Constants.SHORT_MASK;
9
10/**
11 * The Icon Binary File vendor specific ANQP Element,
12 * Wi-Fi Alliance Hotspot 2.0 (Release 2) Technical Specification - Version 5.00,
13 * section 4.11
14 */
15public class HSIconFileElement extends ANQPElement {
16
17    public enum StatusCode {Success, FileNotFound, Unspecified}
18
19    private final StatusCode mStatusCode;
20    private final String mType;
21    private final byte[] mIconData;
22
23    public HSIconFileElement(Constants.ANQPElementType infoID, ByteBuffer payload)
24            throws ProtocolException {
25        super(infoID);
26
27        if (payload.remaining() < 4) {
28            throw new ProtocolException("Truncated icon file: " + payload.remaining());
29        }
30
31        int statusID = payload.get() & BYTE_MASK;
32        mStatusCode = statusID < StatusCode.values().length ? StatusCode.values()[statusID] : null;
33        mType = Constants.getPrefixedString(payload, 1, StandardCharsets.US_ASCII);
34
35        int dataLength = payload.getShort() & SHORT_MASK;
36        mIconData = new byte[dataLength];
37        payload.get(mIconData);
38    }
39
40    public StatusCode getStatusCode() {
41        return mStatusCode;
42    }
43
44    public String getType() {
45        return mType;
46    }
47
48    public byte[] getIconData() {
49        return mIconData;
50    }
51
52    @Override
53    public String toString() {
54        return "HSIconFile{" +
55                "statusCode=" + mStatusCode +
56                ", type='" + mType + '\'' +
57                ", iconData=" + mIconData.length + " bytes }";
58    }
59}
60