1 package com.android.anqp.eap;
2 
3 import java.net.ProtocolException;
4 import java.nio.ByteBuffer;
5 import java.nio.ByteOrder;
6 
7 import static com.android.anqp.Constants.BYTE_MASK;
8 import static com.android.anqp.Constants.INT_MASK;
9 import static com.android.anqp.Constants.SHORT_MASK;
10 
11 /**
12  * An EAP authentication parameter, IEEE802.11-2012, table 8-188
13  */
14 public class ExpandedEAPMethod implements AuthParam {
15 
16     private final EAP.AuthInfoID mAuthInfoID;
17     private final int mVendorID;
18     private final long mVendorType;
19 
ExpandedEAPMethod(EAP.AuthInfoID authInfoID, int length, ByteBuffer payload)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 
ExpandedEAPMethod(EAP.AuthInfoID authInfoID, int vendorID, long vendorType)38     public ExpandedEAPMethod(EAP.AuthInfoID authInfoID, int vendorID, long vendorType) {
39         mAuthInfoID = authInfoID;
40         mVendorID = vendorID;
41         mVendorType = vendorType;
42     }
43 
44     @Override
getAuthInfoID()45     public EAP.AuthInfoID getAuthInfoID() {
46         return mAuthInfoID;
47     }
48 
49     @Override
hashCode()50     public int hashCode() {
51         return (mAuthInfoID.hashCode() * 31 + mVendorID) * 31 + (int) mVendorType;
52     }
53 
54     @Override
equals(Object thatObject)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 
getVendorID()66     public int getVendorID() {
67         return mVendorID;
68     }
69 
getVendorType()70     public long getVendorType() {
71         return mVendorType;
72     }
73 
74     @Override
toString()75     public String toString() {
76         return "Auth method " + mAuthInfoID + ", id " + mVendorID + ", type " + mVendorType + "\n";
77     }
78 }
79