1package com.android.server.wifi.anqp.eap;
2
3import java.net.ProtocolException;
4import java.nio.ByteBuffer;
5import java.nio.ByteOrder;
6
7import static com.android.server.wifi.anqp.Constants.BYTE_MASK;
8import static com.android.server.wifi.anqp.Constants.INT_MASK;
9import static com.android.server.wifi.anqp.Constants.SHORT_MASK;
10
11/**
12 * An EAP authentication parameter, IEEE802.11-2012, table 8-188
13 */
14public class ExpandedEAPMethod implements AuthParam {
15
16    private final EAP.AuthInfoID mAuthInfoID;
17    private final int mVendorID;
18    private final long mVendorType;
19
20    public ExpandedEAPMethod(EAP.AuthInfoID authInfoID, int length, ByteBuffer payload)
21            throws ProtocolException {
22        if (length != 7) {
23            throw new ProtocolException("Bad length: " + payload.remaining());
24        }
25
26        mAuthInfoID = authInfoID;
27
28        ByteBuffer vndBuffer = payload.duplicate().order(ByteOrder.BIG_ENDIAN);
29
30        int id = vndBuffer.getShort() & SHORT_MASK;
31        id = (id << Byte.SIZE) | (vndBuffer.get() & BYTE_MASK);
32        mVendorID = id;
33        mVendorType = vndBuffer.getInt() & INT_MASK;
34
35        payload.position(payload.position()+7);
36    }
37
38    public ExpandedEAPMethod(EAP.AuthInfoID authInfoID, int vendorID, long vendorType) {
39        mAuthInfoID = authInfoID;
40        mVendorID = vendorID;
41        mVendorType = vendorType;
42    }
43
44    @Override
45    public EAP.AuthInfoID getAuthInfoID() {
46        return mAuthInfoID;
47    }
48
49    @Override
50    public int hashCode() {
51        return (mAuthInfoID.hashCode() * 31 + mVendorID) * 31 + (int) mVendorType;
52    }
53
54    @Override
55    public boolean equals(Object thatObject) {
56        if (thatObject == this) {
57            return true;
58        } else if (thatObject == null || thatObject.getClass() != ExpandedEAPMethod.class) {
59            return false;
60        } else {
61            ExpandedEAPMethod that = (ExpandedEAPMethod) thatObject;
62            return that.getVendorID() == getVendorID() && that.getVendorType() == getVendorType();
63        }
64    }
65
66    public int getVendorID() {
67        return mVendorID;
68    }
69
70    public long getVendorType() {
71        return mVendorType;
72    }
73
74    @Override
75    public String toString() {
76        return "Auth method " + mAuthInfoID + ", id " + mVendorID + ", type " + mVendorType + "\n";
77    }
78}
79