1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.wifi.hotspot2.anqp.eap;
18 
19 import com.android.internal.annotations.VisibleForTesting;
20 
21 import java.net.ProtocolException;
22 import java.nio.BufferUnderflowException;
23 import java.nio.ByteBuffer;
24 
25 /**
26  * The Inner Authentication EAP Method Type authentication parameter, IEEE802.11-2012, table 8-188.
27  *
28  * Format:
29  * | EAP Method ID |
30  *         1
31  */
32 public class InnerAuthEAP extends AuthParam {
33     @VisibleForTesting
34     public static final int EXPECTED_LENGTH_VALUE = 1;
35 
36     private final int mEAPMethodID;
37 
38     @VisibleForTesting
InnerAuthEAP(int eapMethodID)39     public InnerAuthEAP(int eapMethodID) {
40         super(AuthParam.PARAM_TYPE_INNER_AUTH_EAP_METHOD_TYPE);
41         mEAPMethodID = eapMethodID;
42     }
43 
44     /**
45      * Parse a InnerAuthEAP from the given buffer.
46      *
47      * @param payload The byte buffer to read from
48      * @param length The length of the data
49      * @return {@link InnerAuthEAP}
50      * @throws ProtocolException
51      * @throws BufferUnderflowException
52      */
parse(ByteBuffer payload, int length)53     public static InnerAuthEAP parse(ByteBuffer payload, int length) throws ProtocolException {
54         if (length != EXPECTED_LENGTH_VALUE) {
55             throw new ProtocolException("Invalid length: " + length);
56         }
57         int eapMethodID = payload.get() & 0xFF;
58         return new InnerAuthEAP(eapMethodID);
59     }
60 
getEAPMethodID()61     public int getEAPMethodID() {
62         return mEAPMethodID;
63     }
64 
65     @Override
equals(Object thatObject)66     public boolean equals(Object thatObject) {
67         if (thatObject == this) {
68             return true;
69         }
70         if (!(thatObject instanceof InnerAuthEAP)) {
71             return false;
72         }
73         InnerAuthEAP that = (InnerAuthEAP) thatObject;
74         return mEAPMethodID == that.mEAPMethodID;
75     }
76 
77     @Override
hashCode()78     public int hashCode() {
79         return mEAPMethodID;
80     }
81 
82     @Override
toString()83     public String toString() {
84         return "InnerAuthEAP{mEAPMethodID=" + mEAPMethodID + "}";
85     }
86 }
87