Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0a5b7efc authored by Etan Cohen's avatar Etan Cohen
Browse files

[NAN] Modify arguments from "array, length" to "array"

Arrays carry their own lengths. There's no need to provide mechanism
to 'shave' the array - could be done explicitly by caller if needed.

Bug: 29617160
Change-Id: Ib135aa04145f400163cd1a8908dfca4590b4480d
parent ab9ef459
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -49,8 +49,8 @@ interface IWifiNanManager
    // session API
    void updatePublish(int clientId, int sessionId, in PublishConfig publishConfig);
    void updateSubscribe(int clientId, int sessionId, in SubscribeConfig subscribeConfig);
    void sendMessage(int clientId, int sessionId, int peerId, in byte[] message, int messageLength,
            int messageId, int retryCount);
    void sendMessage(int clientId, int sessionId, int peerId, in byte[] message, int messageId,
        int retryCount);
    void terminateSession(int clientId, int sessionId);
    int startRanging(int clientId, int sessionId, in RttManager.ParcelableRttParams parms);
}
+2 −3
Original line number Diff line number Diff line
@@ -28,10 +28,9 @@ oneway interface IWifiNanSessionCallback
    void onSessionConfigFail(int reason);
    void onSessionTerminated(int reason);

    void onMatch(int peerId, in byte[] serviceSpecificInfo,
            int serviceSpecificInfoLength, in byte[] matchFilter, int matchFilterLength);
    void onMatch(int peerId, in byte[] serviceSpecificInfo, in byte[] matchFilter);

    void onMessageSendSuccess(int messageId);
    void onMessageSendFail(int messageId, int reason);
    void onMessageReceived(int peerId, in byte[] message, int messageLength);
    void onMessageReceived(int peerId, in byte[] message);
}
+37 −161
Original line number Diff line number Diff line
@@ -18,9 +18,12 @@ package android.net.wifi.nan;

import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.Parcel;
import android.os.Parcelable;

import libcore.util.HexEncoding;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.nio.charset.StandardCharsets;
@@ -61,31 +64,16 @@ public class PublishConfig implements Parcelable {
     */
    public final byte[] mServiceName;

    /**
     * @hide
     */
    public final int mServiceSpecificInfoLength;

    /**
     * @hide
     */
    public final byte[] mServiceSpecificInfo;

    /**
     * @hide
     */
    public final int mTxFilterLength;

    /**
     * @hide
     */
    public final byte[] mTxFilter;

    /**
     * @hide
     */
    public final int mRxFilterLength;

    /**
     * @hide
     */
@@ -112,15 +100,11 @@ public class PublishConfig implements Parcelable {
    public final boolean mEnableTerminateNotification;

    private PublishConfig(byte[] serviceName, byte[] serviceSpecificInfo,
            int serviceSpecificInfoLength, byte[] txFilter, int txFilterLength, byte[] rxFilter,
            int rxFilterLength, int publishType, int publichCount, int ttlSec,
            byte[] txFilter, byte[] rxFilter, int publishType, int publichCount, int ttlSec,
            boolean enableTerminateNotification) {
        mServiceName = serviceName;
        mServiceSpecificInfoLength = serviceSpecificInfoLength;
        mServiceSpecificInfo = serviceSpecificInfo;
        mTxFilterLength = txFilterLength;
        mTxFilter = txFilter;
        mRxFilterLength = rxFilterLength;
        mRxFilter = rxFilter;
        mPublishType = publishType;
        mPublishCount = publichCount;
@@ -130,12 +114,10 @@ public class PublishConfig implements Parcelable {

    @Override
    public String toString() {
        return "PublishConfig [mServiceName='" + mServiceName + "', mServiceSpecificInfo='"
                + (new String(mServiceSpecificInfo, 0, mServiceSpecificInfoLength))
                + "', mTxFilter="
                + (new TlvBufferUtils.TlvIterable(0, 1, mTxFilter, mTxFilterLength)).toString()
                + ", mRxFilter="
                + (new TlvBufferUtils.TlvIterable(0, 1, mRxFilter, mRxFilterLength)).toString()
        return "PublishConfig [mServiceName='" + mServiceName + ", mServiceSpecificInfo='" + (
                (mServiceSpecificInfo == null) ? "null" : HexEncoding.encode(mServiceSpecificInfo))
                + ", mTxFilter=" + (new TlvBufferUtils.TlvIterable(0, 1, mTxFilter)).toString()
                + ", mRxFilter=" + (new TlvBufferUtils.TlvIterable(0, 1, mRxFilter)).toString()
                + ", mPublishType=" + mPublishType + ", mPublishCount=" + mPublishCount
                + ", mTtlSec=" + mTtlSec + ", mEnableTerminateNotification="
                + mEnableTerminateNotification + "]";
@@ -148,22 +130,10 @@ public class PublishConfig implements Parcelable {

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(mServiceName.length);
        if (mServiceName.length != 0) {
        dest.writeByteArray(mServiceName);
        }
        dest.writeInt(mServiceSpecificInfoLength);
        if (mServiceSpecificInfoLength != 0) {
            dest.writeByteArray(mServiceSpecificInfo, 0, mServiceSpecificInfoLength);
        }
        dest.writeInt(mTxFilterLength);
        if (mTxFilterLength != 0) {
            dest.writeByteArray(mTxFilter, 0, mTxFilterLength);
        }
        dest.writeInt(mRxFilterLength);
        if (mRxFilterLength != 0) {
            dest.writeByteArray(mRxFilter, 0, mRxFilterLength);
        }
        dest.writeByteArray(mServiceSpecificInfo);
        dest.writeByteArray(mTxFilter);
        dest.writeByteArray(mRxFilter);
        dest.writeInt(mPublishType);
        dest.writeInt(mPublishCount);
        dest.writeInt(mTtlSec);
@@ -178,34 +148,17 @@ public class PublishConfig implements Parcelable {

        @Override
        public PublishConfig createFromParcel(Parcel in) {
            int serviceNameLength = in.readInt();
            byte[] serviceName = new byte[serviceNameLength];
            if (serviceNameLength != 0) {
                in.readByteArray(serviceName);
            }
            int ssiLength = in.readInt();
            byte[] ssi = new byte[ssiLength];
            if (ssiLength != 0) {
                in.readByteArray(ssi);
            }
            int txFilterLength = in.readInt();
            byte[] txFilter = new byte[txFilterLength];
            if (txFilterLength != 0) {
                in.readByteArray(txFilter);
            }
            int rxFilterLength = in.readInt();
            byte[] rxFilter = new byte[rxFilterLength];
            if (rxFilterLength != 0) {
                in.readByteArray(rxFilter);
            }
            byte[] serviceName = in.createByteArray();
            byte[] ssi = in.createByteArray();
            byte[] txFilter = in.createByteArray();
            byte[] rxFilter = in.createByteArray();
            int publishType = in.readInt();
            int publishCount = in.readInt();
            int ttlSec = in.readInt();
            boolean enableTerminateNotification = in.readInt() != 0;

            return new PublishConfig(serviceName, ssi, ssiLength, txFilter, txFilterLength,
                    rxFilter, rxFilterLength, publishType, publishCount, ttlSec,
                    enableTerminateNotification);
            return new PublishConfig(serviceName, ssi, txFilter, rxFilter, publishType,
                    publishCount, ttlSec, enableTerminateNotification);
        }
    };

@@ -221,45 +174,10 @@ public class PublishConfig implements Parcelable {

        PublishConfig lhs = (PublishConfig) o;

        if (!Arrays.equals(mServiceName, lhs.mServiceName)
                || mServiceSpecificInfoLength != lhs.mServiceSpecificInfoLength
                || mTxFilterLength != lhs.mTxFilterLength
                || mRxFilterLength != lhs.mRxFilterLength) {
            return false;
        }

        if (mServiceSpecificInfo != null && lhs.mServiceSpecificInfo != null) {
            for (int i = 0; i < mServiceSpecificInfoLength; ++i) {
                if (mServiceSpecificInfo[i] != lhs.mServiceSpecificInfo[i]) {
                    return false;
                }
            }
        } else if (mServiceSpecificInfoLength != 0) {
            return false; // invalid != invalid
        }

        if (mTxFilter != null && lhs.mTxFilter != null) {
            for (int i = 0; i < mTxFilterLength; ++i) {
                if (mTxFilter[i] != lhs.mTxFilter[i]) {
                    return false;
                }
            }
        } else if (mTxFilterLength != 0) {
            return false; // invalid != invalid
        }

        if (mRxFilter != null && lhs.mRxFilter != null) {
            for (int i = 0; i < mRxFilterLength; ++i) {
                if (mRxFilter[i] != lhs.mRxFilter[i]) {
                    return false;
                }
            }
        } else if (mRxFilterLength != 0) {
            return false; // invalid != invalid
        }

        return mPublishType == lhs.mPublishType && mPublishCount == lhs.mPublishCount
                && mTtlSec == lhs.mTtlSec
        return Arrays.equals(mServiceName, lhs.mServiceName) && Arrays.equals(mServiceSpecificInfo,
                lhs.mServiceSpecificInfo) && Arrays.equals(mTxFilter, lhs.mTxFilter)
                && Arrays.equals(mRxFilter, lhs.mRxFilter) && mPublishType == lhs.mPublishType
                && mPublishCount == lhs.mPublishCount && mTtlSec == lhs.mTtlSec
                && mEnableTerminateNotification == lhs.mEnableTerminateNotification;
    }

@@ -268,11 +186,8 @@ public class PublishConfig implements Parcelable {
        int result = 17;

        result = 31 * result + Arrays.hashCode(mServiceName);
        result = 31 * result + mServiceSpecificInfoLength;
        result = 31 * result + Arrays.hashCode(mServiceSpecificInfo);
        result = 31 * result + mTxFilterLength;
        result = 31 * result + Arrays.hashCode(mTxFilter);
        result = 31 * result + mRxFilterLength;
        result = 31 * result + Arrays.hashCode(mRxFilter);
        result = 31 * result + mPublishType;
        result = 31 * result + mPublishCount;
@@ -291,24 +206,11 @@ public class PublishConfig implements Parcelable {
    public void validate() throws IllegalArgumentException {
        WifiNanUtils.validateServiceName(mServiceName);

        if (mServiceSpecificInfoLength != 0 && (mServiceSpecificInfo == null
                || mServiceSpecificInfo.length < mServiceSpecificInfoLength)) {
            throw new IllegalArgumentException("Non-matching combination of "
                    + "serviceSpecificInfo and serviceSpecificInfoLength");
        }
        if (mTxFilterLength != 0 && (mTxFilter == null || mTxFilter.length < mTxFilterLength)) {
            throw new IllegalArgumentException(
                    "Non-matching combination of txFilter and txFilterLength");
        }
        if (!TlvBufferUtils.isValid(mTxFilter, mTxFilterLength, 0, 1)) {
        if (!TlvBufferUtils.isValid(mTxFilter, 0, 1)) {
            throw new IllegalArgumentException(
                    "Invalid txFilter configuration - LV fields do not match up to length");
        }
        if (mRxFilterLength != 0 && (mRxFilter == null || mRxFilter.length < mRxFilterLength)) {
            throw new IllegalArgumentException(
                    "Non-matching combination of rxFilter and rxFilterLength");
        }
        if (!TlvBufferUtils.isValid(mRxFilter, mRxFilterLength, 0, 1)) {
        if (!TlvBufferUtils.isValid(mRxFilter, 0, 1)) {
            throw new IllegalArgumentException(
                    "Invalid rxFilter configuration - LV fields do not match up to length");
        }
@@ -321,11 +223,13 @@ public class PublishConfig implements Parcelable {
        if (mTtlSec < 0) {
            throw new IllegalArgumentException("Invalid ttlSec - must be non-negative");
        }
        if (mPublishType == PublishConfig.PUBLISH_TYPE_UNSOLICITED && mRxFilterLength != 0) {
        if (mPublishType == PublishConfig.PUBLISH_TYPE_UNSOLICITED && mRxFilter != null
                && mRxFilter.length != 0) {
            throw new IllegalArgumentException("Invalid publish config: UNSOLICITED "
                    + "publishes (active) can't have an Rx filter");
        }
        if (mPublishType == PublishConfig.PUBLISH_TYPE_SOLICITED && mTxFilterLength != 0) {
        if (mPublishType == PublishConfig.PUBLISH_TYPE_SOLICITED && mTxFilter != null
                && mTxFilter.length != 0) {
            throw new IllegalArgumentException("Invalid publish config: SOLICITED "
                    + "publishes (passive) can't have a Tx filter");
        }
@@ -336,12 +240,9 @@ public class PublishConfig implements Parcelable {
     */
    public static final class Builder {
        private byte[] mServiceName;
        private int mServiceSpecificInfoLength;
        private byte[] mServiceSpecificInfo = new byte[0];
        private int mTxFilterLength;
        private byte[] mTxFilter = new byte[0];
        private int mRxFilterLength;
        private byte[] mRxFilter = new byte[0];
        private byte[] mServiceSpecificInfo;
        private byte[] mTxFilter;
        private byte[] mRxFilter;
        private int mPublishType = PUBLISH_TYPE_UNSOLICITED;
        private int mPublishCount = 0;
        private int mTtlSec = 0;
@@ -377,26 +278,17 @@ public class PublishConfig implements Parcelable {
         *
         * @param serviceSpecificInfo A byte-array for the service-specific
         *            information field.
         * @param serviceSpecificInfoLength The length of the byte-array to be
         *            used.
         * @return The builder to facilitate chaining
         *         {@code builder.setXXX(..).setXXX(..)}.
         */
        public Builder setServiceSpecificInfo(byte[] serviceSpecificInfo,
                int serviceSpecificInfoLength) {
            if (serviceSpecificInfoLength != 0 && (serviceSpecificInfo == null
                    || serviceSpecificInfo.length < serviceSpecificInfoLength)) {
                throw new IllegalArgumentException("Non-matching combination of "
                        + "serviceSpecificInfo and serviceSpecificInfoLength");
            }
            mServiceSpecificInfoLength = serviceSpecificInfoLength;
        public Builder setServiceSpecificInfo(@Nullable byte[] serviceSpecificInfo) {
            mServiceSpecificInfo = serviceSpecificInfo;
            return this;
        }

        /**
         * Specify service specific information for the publish session - same
         * as {@link PublishConfig.Builder#setServiceSpecificInfo(byte[], int)}
         * as {@link PublishConfig.Builder#setServiceSpecificInfo(byte[])}
         * but obtaining the data from a String.
         *
         * @param serviceSpecificInfoStr The service specific information string
@@ -407,7 +299,6 @@ public class PublishConfig implements Parcelable {
         */
        public Builder setServiceSpecificInfo(@NonNull String serviceSpecificInfoStr) {
            mServiceSpecificInfo = serviceSpecificInfoStr.getBytes();
            mServiceSpecificInfoLength = mServiceSpecificInfo.length;
            return this;
        }

@@ -424,18 +315,11 @@ public class PublishConfig implements Parcelable {
         *
         * @param txFilter The byte-array containing the LV formatted transmit
         *            filter.
         * @param txFilterLength The number of bytes in the transmit filter
         *            argument.
         * @return The builder to facilitate chaining
         *         {@code builder.setXXX(..).setXXX(..)}.
         */
        public Builder setTxFilter(byte[] txFilter, int txFilterLength) {
            if (txFilterLength != 0 && (txFilter == null || txFilter.length < txFilterLength)) {
                throw new IllegalArgumentException(
                        "Non-matching combination of txFilter and txFilterLength");
            }
        public Builder setTxFilter(@Nullable byte[] txFilter) {
            mTxFilter = txFilter;
            mTxFilterLength = txFilterLength;
            return this;
        }

@@ -452,18 +336,11 @@ public class PublishConfig implements Parcelable {
         *
         * @param rxFilter The byte-array containing the LV formatted receive
         *            filter.
         * @param rxFilterLength The number of bytes in the receive filter
         *            argument.
         * @return The builder to facilitate chaining
         *         {@code builder.setXXX(..).setXXX(..)}.
         */
        public Builder setRxFilter(byte[] rxFilter, int rxFilterLength) {
            if (rxFilterLength != 0 && (rxFilter == null || rxFilter.length < rxFilterLength)) {
                throw new IllegalArgumentException(
                        "Non-matching combination of rxFilter and rxFilterLength");
            }
        public Builder setRxFilter(@Nullable byte[] rxFilter) {
            mRxFilter = rxFilter;
            mRxFilterLength = rxFilterLength;
            return this;
        }

@@ -547,9 +424,8 @@ public class PublishConfig implements Parcelable {
         * builder.
         */
        public PublishConfig build() {
            return new PublishConfig(mServiceName, mServiceSpecificInfo, mServiceSpecificInfoLength,
                    mTxFilter, mTxFilterLength, mRxFilter, mRxFilterLength, mPublishType,
                    mPublishCount, mTtlSec, mEnableTerminateNotification);
            return new PublishConfig(mServiceName, mServiceSpecificInfo, mTxFilter, mRxFilter,
                    mPublishType, mPublishCount, mTtlSec, mEnableTerminateNotification);
        }
    }
}
+40 −163

File changed.

Preview size limit exceeded, changes collapsed.

+34 −33
Original line number Diff line number Diff line
@@ -16,10 +16,13 @@

package android.net.wifi.nan;

import android.annotation.Nullable;

import libcore.io.Memory;

import java.nio.BufferOverflowException;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.Iterator;

/**
@@ -50,8 +53,7 @@ public class TlvBufferUtils {
     * Values are added to the structure using the {@code TlvConstructor.put*()}
     * methods.
     * <p>
     * The final byte array is obtained using {@link TlvConstructor#getArray()}
     * and {@link TlvConstructor#getActualLength()} methods.
     * The final byte array is obtained using {@link TlvConstructor#getArray()}.
     */
    public static class TlvConstructor {
        private int mTypeSize;
@@ -88,9 +90,9 @@ public class TlvBufferUtils {
         * @return The constructor to facilitate chaining
         *         {@code ctr.putXXX(..).putXXX(..)}.
         */
        public TlvConstructor wrap(byte[] array) {
        public TlvConstructor wrap(@Nullable byte[] array) {
            mArray = array;
            mArrayLength = array.length;
            mArrayLength = (array == null) ? 0 : array.length;
            return this;
        }

@@ -137,10 +139,13 @@ public class TlvBufferUtils {
         * @return The constructor to facilitate chaining
         *         {@code ctr.putXXX(..).putXXX(..)}.
         */
        public TlvConstructor putByteArray(int type, byte[] array, int offset, int length) {
        public TlvConstructor putByteArray(int type, @Nullable byte[] array, int offset,
                int length) {
            checkLength(length);
            addHeader(type, length);
            if (length != 0) {
                System.arraycopy(array, offset, mArray, mPosition, length);
            }
            mPosition += length;
            return this;
        }
@@ -155,8 +160,8 @@ public class TlvBufferUtils {
         * @return The constructor to facilitate chaining
         *         {@code ctr.putXXX(..).putXXX(..)}.
         */
        public TlvConstructor putByteArray(int type, byte[] array) {
            return putByteArray(type, array, 0, array.length);
        public TlvConstructor putByteArray(int type, @Nullable byte[] array) {
            return putByteArray(type, array, 0, (array == null) ? 0 : array.length);
        }

        /**
@@ -223,24 +228,25 @@ public class TlvBufferUtils {
         * @return The constructor to facilitate chaining
         *         {@code ctr.putXXX(..).putXXX(..)}.
         */
        public TlvConstructor putString(int type, String data) {
            byte[] bytes = data.getBytes();
            return putByteArray(type, bytes, 0, bytes.length);
        public TlvConstructor putString(int type, @Nullable String data) {
            byte[] bytes = null;
            int length = 0;
            if (data != null) {
                bytes = data.getBytes();
                length = bytes.length;
            }
            return putByteArray(type, bytes, 0, length);
        }

        /**
         * Returns the constructed TLV formatted byte-array. Note that the
         * returned array is the fully wrapped (
         * {@link TlvConstructor#wrap(byte[])}) or allocated (
         * {@link TlvConstructor#allocate(int)}) array - which isn't necessarily
         * the actual size of the formatted data. Use
         * {@link TlvConstructor#getActualLength()} to obtain the size of the
         * formatted data.
         * Returns the constructed TLV formatted byte-array. This array is a copy of the wrapped
         * or allocated array - truncated to just the significant bytes - i.e. those written into
         * the (T)LV.
         *
         * @return The byte array containing the TLV formatted structure.
         */
        public byte[] getArray() {
            return mArray;
            return Arrays.copyOf(mArray, getActualLength());
        }

        /**
@@ -250,7 +256,7 @@ public class TlvBufferUtils {
         *
         * @return The size of the TLV formatted portion of the byte array.
         */
        public int getActualLength() {
        private int getActualLength() {
            return mPosition;
        }

@@ -307,7 +313,7 @@ public class TlvBufferUtils {
         */
        public int mOffset;

        private TlvElement(int type, int length, byte[] refArray, int offset) {
        private TlvElement(int type, int length, @Nullable byte[] refArray, int offset) {
            mType = type;
            mLength = length;
            mRefArray = refArray;
@@ -389,10 +395,8 @@ public class TlvBufferUtils {
         * @param lengthSize Number of bytes sued for the Length (L) field.
         *            Values values are 1 or 2 bytes.
         * @param array The TLV formatted byte-array to parse.
         * @param length The number of bytes of the array to be used in the
         *            parsing.
         */
        public TlvIterable(int typeSize, int lengthSize, byte[] array, int length) {
        public TlvIterable(int typeSize, int lengthSize, @Nullable byte[] array) {
            if (typeSize < 0 || typeSize > 2 || lengthSize <= 0 || lengthSize > 2) {
                throw new IllegalArgumentException(
                        "Invalid sizes - typeSize=" + typeSize + ", lengthSize=" + lengthSize);
@@ -400,7 +404,7 @@ public class TlvBufferUtils {
            mTypeSize = typeSize;
            mLengthSize = lengthSize;
            mArray = array;
            mArrayLength = length;
            mArrayLength = (array == null) ? 0 : array.length;
        }

        /**
@@ -494,12 +498,11 @@ public class TlvBufferUtils {
     * fields correctly fill the specified length (and do not overshoot).
     *
     * @param array The (T)LV array to verify.
     * @param length The number of bytes in the array to consider (starting at offset 0).
     * @param typeSize The size (in bytes) of the type field. Valid values are 0, 1, or 2.
     * @param lengthSize The size (in bytes) of the length field. Valid values are 1 or 2.
     * @return A boolean indicating whether the array is valid (true) or invalid (false).
     */
    public static boolean isValid(byte[] array, int length, int typeSize, int lengthSize) {
    public static boolean isValid(@Nullable byte[] array, int typeSize, int lengthSize) {
        if (typeSize < 0 || typeSize > 2) {
            throw new IllegalArgumentException(
                    "Invalid arguments - typeSize must be 0, 1, or 2: typeSize=" + typeSize);
@@ -508,14 +511,12 @@ public class TlvBufferUtils {
            throw new IllegalArgumentException(
                    "Invalid arguments - lengthSize must be 1 or 2: lengthSize=" + lengthSize);
        }
        if (length < 0 || length > array.length) {
            throw new IllegalArgumentException(
                    "Invalid arguments - length must be non-negative and <= array.length: length="
                            + length + ", array.length=" + array.length);
        if (array == null) {
            return true;
        }

        int nextTlvIndex = 0;
        while (nextTlvIndex + typeSize + lengthSize <= length) {
        while (nextTlvIndex + typeSize + lengthSize <= array.length) {
            nextTlvIndex += typeSize;
            if (lengthSize == 1) {
                nextTlvIndex += lengthSize + array[nextTlvIndex];
@@ -525,6 +526,6 @@ public class TlvBufferUtils {
            }
        }

        return nextTlvIndex == length;
        return nextTlvIndex == array.length;
    }
}
Loading