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.nio.BufferUnderflowException;
23 import java.nio.ByteBuffer;
24 import java.nio.charset.StandardCharsets;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 
29 /**
30  * The Domain Name ANQP Element, IEEE802.11-2012 section 8.4.4.15.
31  *
32  * Format:
33  * | Domain Name Field #1 (optional) | ...
34  *            variable
35  *
36  * Domain Name Field Format:
37  * | Length | Domain Name |
38  *      1       variable
39  */
40 public class DomainNameElement extends ANQPElement {
41     private final List<String> mDomains;
42 
43     @VisibleForTesting
DomainNameElement(List<String> domains)44     public DomainNameElement(List<String> domains) {
45         super(Constants.ANQPElementType.ANQPDomName);
46         mDomains = domains;
47     }
48 
49     /**
50      * Parse a DomainNameElement from the given buffer.
51      *
52      * @param payload The byte buffer to read from
53      * @return {@link DomainNameElement}
54      * @throws BufferUnderflowException
55      */
parse(ByteBuffer payload)56     public static DomainNameElement parse(ByteBuffer payload) {
57         List<String> domains = new ArrayList<>();
58         while (payload.hasRemaining()) {
59             // Use latin-1 to decode for now - safe for ASCII and retains encoding
60             domains.add(ByteBufferReader.readStringWithByteLength(
61                     payload, StandardCharsets.ISO_8859_1));
62         }
63         return new DomainNameElement(domains);
64     }
65 
getDomains()66     public List<String> getDomains() {
67         return Collections.unmodifiableList(mDomains);
68     }
69 
70     @Override
equals(Object thatObject)71     public boolean equals(Object thatObject) {
72         if (this == thatObject) {
73             return true;
74         }
75         if (!(thatObject instanceof DomainNameElement)) {
76             return false;
77         }
78         DomainNameElement that = (DomainNameElement) thatObject;
79         return mDomains.equals(that.mDomains);
80     }
81 
82     @Override
hashCode()83     public int hashCode() {
84         return mDomains.hashCode();
85     }
86 
87     @Override
toString()88     public String toString() {
89         return "DomainName{" +
90                 "mDomains=" + mDomains +
91                 '}';
92     }
93 }
94