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;
18 
19 import com.android.internal.annotations.VisibleForTesting;
20 import com.android.server.wifi.ByteBufferReader;
21 
22 import java.net.ProtocolException;
23 import java.nio.BufferUnderflowException;
24 import java.nio.ByteBuffer;
25 import java.nio.ByteOrder;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
29 
30 
31 /**
32  * The NAI (Network Access Identifier) Realm ANQP Element, IEEE802.11-2012 section 8.4.4.10.
33  *
34  * Format:
35  * | NAI Realm Count (optional) | NAI Realm Data #1 (optional) | ....
36  *             2                         variable
37  */
38 public class NAIRealmElement extends ANQPElement {
39     private final List<NAIRealmData> mRealmDataList;
40 
41     @VisibleForTesting
NAIRealmElement(List<NAIRealmData> realmDataList)42     public NAIRealmElement(List<NAIRealmData> realmDataList) {
43         super(Constants.ANQPElementType.ANQPNAIRealm);
44         mRealmDataList = realmDataList;
45     }
46 
47     /**
48      * Parse a NAIRealmElement from the given buffer.
49      *
50      * @param payload The byte buffer to read from
51      * @return {@link NAIRealmElement}
52      * @throws BufferUnderflowException
53      */
parse(ByteBuffer payload)54     public static NAIRealmElement parse(ByteBuffer payload)
55             throws ProtocolException {
56         List<NAIRealmData> realmDataList = new ArrayList<>();
57         if (payload.hasRemaining()) {
58             int count = (int) ByteBufferReader.readInteger(payload, ByteOrder.LITTLE_ENDIAN, 2)
59                     & 0xFFFF;
60             while (count > 0) {
61                 realmDataList.add(NAIRealmData.parse(payload));
62                 count--;
63             }
64         }
65         return new NAIRealmElement(realmDataList);
66     }
67 
getRealmDataList()68     public List<NAIRealmData> getRealmDataList() {
69         return Collections.unmodifiableList(mRealmDataList);
70     }
71 
72     @Override
equals(Object thatObject)73     public boolean equals(Object thatObject) {
74         if (this == thatObject) {
75             return true;
76         }
77         if (!(thatObject instanceof NAIRealmElement)) {
78             return false;
79         }
80         NAIRealmElement that = (NAIRealmElement) thatObject;
81         return mRealmDataList.equals(that.mRealmDataList);
82     }
83 
84     @Override
hashCode()85     public int hashCode() {
86         return mRealmDataList.hashCode();
87     }
88 
89     @Override
toString()90     public String toString() {
91         return "NAIRealmElement{mRealmDataList=" + mRealmDataList + "}";
92     }
93 }
94