Home | History | Annotate | Download | only in anqp
      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 
     21 import java.net.ProtocolException;
     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 Venue Name ANQP Element, IEEE802.11-2012 section 8.4.4.4.
     31  *
     32  * Format:
     33  *
     34  * | Venue Info | Venue Name Duple #1 (optional) | ...
     35  *      2                  variable
     36  *
     37  * | Venue Name Duple #N (optional) |
     38  *             variable
     39  *
     40  * Refer to {@link I18Name} for the format of the Venue Name Duple
     41  * fields.
     42  */
     43 public class VenueNameElement extends ANQPElement {
     44     @VisibleForTesting
     45     public static final int VENUE_INFO_LENGTH = 2;
     46 
     47     /**
     48      * Maximum length for a Venue Name.  Refer to IEEE802.11-2012 section 8.4.4.4 for more info.
     49      */
     50     @VisibleForTesting
     51     public static final int MAXIMUM_VENUE_NAME_LENGTH = 252;
     52 
     53     private final List<I18Name> mNames;
     54 
     55     @VisibleForTesting
     56     public VenueNameElement(List<I18Name> names) {
     57         super(Constants.ANQPElementType.ANQPVenueName);
     58         mNames = names;
     59     }
     60 
     61     /**
     62      * Parse a VenueNameElement from the given buffer.
     63      *
     64      * @param payload The byte buffer to read from
     65      * @return {@link VenueNameElement}
     66      * @throws BufferUnderflowException
     67      * @throws ProtocolException
     68      */
     69     public static VenueNameElement parse(ByteBuffer payload)
     70             throws ProtocolException {
     71         // Skip the Venue Info field, which we don't use.
     72         for (int i = 0; i < VENUE_INFO_LENGTH; ++i) {
     73             payload.get();
     74         }
     75 
     76         List<I18Name> names = new ArrayList<I18Name>();
     77         while (payload.hasRemaining()) {
     78             I18Name name = I18Name.parse(payload);
     79             // Verify that the number of octets for the venue name doesn't exceed the max allowed.
     80             int textBytes = name.getText().getBytes(StandardCharsets.UTF_8).length;
     81             if (textBytes > MAXIMUM_VENUE_NAME_LENGTH) {
     82                 throw new ProtocolException("Venue Name exceeds the maximum allowed " + textBytes);
     83             }
     84             names.add(name);
     85         }
     86         return new VenueNameElement(names);
     87     }
     88 
     89     public List<I18Name> getNames() {
     90         return Collections.unmodifiableList(mNames);
     91     }
     92 
     93     @Override
     94     public boolean equals(Object thatObject) {
     95         if (this == thatObject) {
     96             return true;
     97         }
     98         if (!(thatObject instanceof VenueNameElement)) {
     99             return false;
    100         }
    101         VenueNameElement that = (VenueNameElement) thatObject;
    102         return mNames.equals(that.mNames);
    103     }
    104 
    105     @Override
    106     public int hashCode() {
    107         return mNames.hashCode();
    108     }
    109 
    110     @Override
    111     public String toString() {
    112         return "VenueName{ mNames=" + mNames + "}";
    113     }
    114 
    115 }
    116