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 import com.android.server.wifi.ByteBufferReader;
     21 
     22 import java.nio.BufferUnderflowException;
     23 import java.nio.ByteBuffer;
     24 import java.nio.ByteOrder;
     25 
     26 /**
     27  * The ProtoPort Tuple used by Connection Capability vendor specific ANQP Element,
     28  * Wi-Fi Alliance Hotspot 2.0 (Release 2) Technical Specification - Version 5.00,
     29  * section 4.5
     30  *
     31  * Format:
     32  * | IP Procotol | Port Number | Status |
     33  *        1             2           1
     34  */
     35 public class ProtocolPortTuple {
     36     /**
     37      * Number of raw bytes needed for the tuple.
     38      */
     39     @VisibleForTesting
     40     public static final int RAW_BYTE_SIZE = 4;
     41 
     42     public static final int PROTO_STATUS_CLOSED = 0;
     43     public static final int PROTO_STATUS_OPEN = 1;
     44     public static final int PROTO_STATUS_UNKNOWN = 2;
     45 
     46     private final int mProtocol;
     47     private final int mPort;
     48     private final int mStatus;
     49 
     50     @VisibleForTesting
     51     public ProtocolPortTuple(int protocol, int port, int status) {
     52         mProtocol = protocol;
     53         mPort = port;
     54         mStatus = status;
     55     }
     56 
     57     /**
     58      * Parse a ProtocolPortTuple from the given buffer.
     59      *
     60      * @param payload The byte buffer to read from
     61      * @return {@link ProtocolPortTuple}
     62      * @throws BufferUnderflowException
     63      */
     64     public static ProtocolPortTuple parse(ByteBuffer payload) {
     65         int protocol = payload.get();
     66         int port = (int) ByteBufferReader.readInteger(payload, ByteOrder.LITTLE_ENDIAN, 2)
     67                 & 0xFFFF;
     68         int status = payload.get() & 0xFF;
     69         return new ProtocolPortTuple(protocol, port, status);
     70     }
     71 
     72     public int getProtocol() {
     73         return mProtocol;
     74     }
     75 
     76     public int getPort() {
     77         return mPort;
     78     }
     79 
     80     public int getStatus() {
     81         return mStatus;
     82     }
     83 
     84     @Override
     85     public boolean equals(Object thatObject) {
     86         if (this == thatObject) {
     87             return true;
     88         }
     89         if (!(thatObject instanceof ProtocolPortTuple)) {
     90             return false;
     91         }
     92         ProtocolPortTuple that = (ProtocolPortTuple) thatObject;
     93         return mProtocol == that.mProtocol
     94                 && mPort == that.mPort
     95                 && mStatus == that.mStatus;
     96     }
     97 
     98     @Override
     99     public int hashCode() {
    100         return (mProtocol * 31 + mPort) * 31 + mStatus;
    101     }
    102 
    103     @Override
    104     public String toString() {
    105         return "ProtocolTuple{" + "mProtocol=" + mProtocol + ", mPort=" + mPort
    106                 + ", mStatus=" + mStatus + '}';
    107     }
    108 }
    109