HSOsuProvidersElement.java revision efe18a90941645b907fa2b50d922e7373303613e
1/*
2 * Copyright (C) 2017 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
17package com.android.server.wifi.hotspot2.anqp;
18
19import android.net.wifi.WifiSsid;
20
21import com.android.internal.annotations.VisibleForTesting;
22
23import java.net.ProtocolException;
24import java.nio.BufferUnderflowException;
25import java.nio.ByteBuffer;
26import java.util.ArrayList;
27import java.util.Collections;
28import java.util.List;
29import java.util.Objects;
30
31/**
32 * The OSU Providers List vendor specific ANQP Element,
33 * Wi-Fi Alliance Hotspot 2.0 (Release 2) Technical Specification - Version 5.00,
34 * section 4.8.
35 *
36 * Format:
37 *
38 * | OSU SSID Length | OSU SSID | Number of OSU Providers | Provider #1 | ...
39 *          1          variable             1                 variable
40 *
41 */
42public class HSOsuProvidersElement extends ANQPElement {
43    /**
44     * Maximum length for a SSID.  Refer to IEEE 802.11-2012 Section 8.4.2.2
45     * for more info.
46     */
47    @VisibleForTesting
48    public static final int MAXIMUM_OSU_SSID_LENGTH = 32;
49
50    private final WifiSsid mOsuSsid;
51    private final List<OsuProviderInfo> mProviders;
52
53    @VisibleForTesting
54    public HSOsuProvidersElement(WifiSsid osuSsid, List<OsuProviderInfo> providers) {
55        super(Constants.ANQPElementType.HSOSUProviders);
56        mOsuSsid = osuSsid;
57        mProviders = providers;
58    }
59
60    /**
61     * Parse a HSOsuProvidersElement from the given buffer.
62     *
63     * @param payload The buffer to read from
64     * @return {@link HSOsuProvidersElement}
65     * @throws BufferUnderflowException
66     * @throws ProtocolException
67     */
68    public static HSOsuProvidersElement parse(ByteBuffer payload)
69            throws ProtocolException {
70        int ssidLength = payload.get() & 0xFF;
71        if (ssidLength > MAXIMUM_OSU_SSID_LENGTH) {
72            throw new ProtocolException("Invalid SSID length: " + ssidLength);
73        }
74        byte[] ssidBytes = new byte[ssidLength];
75        payload.get(ssidBytes);
76
77        int numProviders = payload.get() & 0xFF;
78        List<OsuProviderInfo> providers = new ArrayList<>();
79        while (numProviders > 0) {
80            providers.add(OsuProviderInfo.parse(payload));
81            numProviders--;
82        }
83
84        return new HSOsuProvidersElement(WifiSsid.createFromByteArray(ssidBytes), providers);
85    }
86
87    public WifiSsid getOsuSsid() {
88        return mOsuSsid;
89    }
90
91    public List<OsuProviderInfo> getProviders() {
92        return Collections.unmodifiableList(mProviders);
93    }
94
95    @Override
96    public boolean equals(Object thatObject) {
97        if (this == thatObject) {
98            return true;
99        }
100        if (!(thatObject instanceof HSOsuProvidersElement)) {
101            return false;
102        }
103        HSOsuProvidersElement that = (HSOsuProvidersElement) thatObject;
104        return (mOsuSsid == null ? that.mOsuSsid == null : mOsuSsid.equals(that.mOsuSsid))
105                && (mProviders == null ? that.mProviders == null
106                        : mProviders.equals(that.mProviders));
107    }
108
109    @Override
110    public int hashCode() {
111        return Objects.hash(mOsuSsid, mProviders);
112    }
113
114    @Override
115    public String toString() {
116        return "OSUProviders{" + "mOsuSsid=" + mOsuSsid + ", mProviders=" + mProviders + "}";
117    }
118}
119