Home | History | Annotate | Download | only in anqp
      1 package com.android.server.wifi.hotspot2.anqp;
      2 
      3 import com.android.internal.annotations.VisibleForTesting;
      4 import com.android.server.wifi.ByteBufferReader;
      5 import com.android.server.wifi.hotspot2.Utils;
      6 
      7 import java.net.ProtocolException;
      8 import java.nio.BufferUnderflowException;
      9 import java.nio.ByteBuffer;
     10 import java.nio.ByteOrder;
     11 import java.util.ArrayList;
     12 import java.util.Collections;
     13 import java.util.List;
     14 
     15 /**
     16  * The Roaming Consortium ANQP Element, IEEE802.11-2012 section 8.4.4.7
     17  *
     18  ** Format:
     19  *
     20  * | OI Duple #1 (optional) | ...
     21  *         variable
     22  *
     23  * | OI Length |     OI     |
     24  *       1        variable
     25  *
     26  */
     27 public class RoamingConsortiumElement extends ANQPElement {
     28     @VisibleForTesting
     29     public static final int MINIMUM_OI_LENGTH = Byte.BYTES;
     30 
     31     @VisibleForTesting
     32     public static final int MAXIMUM_OI_LENGTH = Long.BYTES;
     33 
     34     private final List<Long> mOIs;
     35 
     36     @VisibleForTesting
     37     public RoamingConsortiumElement(List<Long> ois) {
     38         super(Constants.ANQPElementType.ANQPRoamingConsortium);
     39         mOIs = ois;
     40     }
     41 
     42     /**
     43      * Parse a VenueNameElement from the given payload.
     44      *
     45      * @param payload The byte buffer to read from
     46      * @return {@link RoamingConsortiumElement}
     47      * @throws BufferUnderflowException
     48      * @throws ProtocolException
     49      */
     50     public static RoamingConsortiumElement parse(ByteBuffer payload)
     51             throws ProtocolException {
     52         List<Long> OIs = new ArrayList<Long>();
     53         while (payload.hasRemaining()) {
     54             int length = payload.get() & 0xFF;
     55             if (length < MINIMUM_OI_LENGTH || length > MAXIMUM_OI_LENGTH) {
     56                 throw new ProtocolException("Bad OI length: " + length);
     57             }
     58             OIs.add(ByteBufferReader.readInteger(payload, ByteOrder.BIG_ENDIAN, length));
     59         }
     60         return new RoamingConsortiumElement(OIs);
     61     }
     62 
     63     public List<Long> getOIs() {
     64         return Collections.unmodifiableList(mOIs);
     65     }
     66 
     67     @Override
     68     public boolean equals(Object thatObject) {
     69         if (this == thatObject) {
     70             return true;
     71         }
     72         if (!(thatObject instanceof RoamingConsortiumElement)) {
     73             return false;
     74         }
     75         RoamingConsortiumElement that = (RoamingConsortiumElement) thatObject;
     76         return mOIs.equals(that.mOIs);
     77     }
     78 
     79     @Override
     80     public int hashCode() {
     81         return mOIs.hashCode();
     82     }
     83 
     84     @Override
     85     public String toString() {
     86         return "RoamingConsortium{mOis=[" + Utils.roamingConsortiumsToString(mOIs) + "]}";
     87     }
     88 }
     89