[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
This commit is contained in:
Etan Cohen
2016-07-15 15:28:39 -07:00
parent ab9ef45971
commit 0a5b7efcb2
8 changed files with 160 additions and 443 deletions

View File

@@ -49,8 +49,8 @@ interface IWifiNanManager
// session API // session API
void updatePublish(int clientId, int sessionId, in PublishConfig publishConfig); void updatePublish(int clientId, int sessionId, in PublishConfig publishConfig);
void updateSubscribe(int clientId, int sessionId, in SubscribeConfig subscribeConfig); void updateSubscribe(int clientId, int sessionId, in SubscribeConfig subscribeConfig);
void sendMessage(int clientId, int sessionId, int peerId, in byte[] message, int messageLength, void sendMessage(int clientId, int sessionId, int peerId, in byte[] message, int messageId,
int messageId, int retryCount); int retryCount);
void terminateSession(int clientId, int sessionId); void terminateSession(int clientId, int sessionId);
int startRanging(int clientId, int sessionId, in RttManager.ParcelableRttParams parms); int startRanging(int clientId, int sessionId, in RttManager.ParcelableRttParams parms);
} }

View File

@@ -28,10 +28,9 @@ oneway interface IWifiNanSessionCallback
void onSessionConfigFail(int reason); void onSessionConfigFail(int reason);
void onSessionTerminated(int reason); void onSessionTerminated(int reason);
void onMatch(int peerId, in byte[] serviceSpecificInfo, void onMatch(int peerId, in byte[] serviceSpecificInfo, in byte[] matchFilter);
int serviceSpecificInfoLength, in byte[] matchFilter, int matchFilterLength);
void onMessageSendSuccess(int messageId); void onMessageSendSuccess(int messageId);
void onMessageSendFail(int messageId, int reason); void onMessageSendFail(int messageId, int reason);
void onMessageReceived(int peerId, in byte[] message, int messageLength); void onMessageReceived(int peerId, in byte[] message);
} }

View File

@@ -18,9 +18,12 @@ package android.net.wifi.nan;
import android.annotation.IntDef; import android.annotation.IntDef;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import libcore.util.HexEncoding;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@@ -61,31 +64,16 @@ public class PublishConfig implements Parcelable {
*/ */
public final byte[] mServiceName; public final byte[] mServiceName;
/**
* @hide
*/
public final int mServiceSpecificInfoLength;
/** /**
* @hide * @hide
*/ */
public final byte[] mServiceSpecificInfo; public final byte[] mServiceSpecificInfo;
/**
* @hide
*/
public final int mTxFilterLength;
/** /**
* @hide * @hide
*/ */
public final byte[] mTxFilter; public final byte[] mTxFilter;
/**
* @hide
*/
public final int mRxFilterLength;
/** /**
* @hide * @hide
*/ */
@@ -112,15 +100,11 @@ public class PublishConfig implements Parcelable {
public final boolean mEnableTerminateNotification; public final boolean mEnableTerminateNotification;
private PublishConfig(byte[] serviceName, byte[] serviceSpecificInfo, private PublishConfig(byte[] serviceName, byte[] serviceSpecificInfo,
int serviceSpecificInfoLength, byte[] txFilter, int txFilterLength, byte[] rxFilter, byte[] txFilter, byte[] rxFilter, int publishType, int publichCount, int ttlSec,
int rxFilterLength, int publishType, int publichCount, int ttlSec,
boolean enableTerminateNotification) { boolean enableTerminateNotification) {
mServiceName = serviceName; mServiceName = serviceName;
mServiceSpecificInfoLength = serviceSpecificInfoLength;
mServiceSpecificInfo = serviceSpecificInfo; mServiceSpecificInfo = serviceSpecificInfo;
mTxFilterLength = txFilterLength;
mTxFilter = txFilter; mTxFilter = txFilter;
mRxFilterLength = rxFilterLength;
mRxFilter = rxFilter; mRxFilter = rxFilter;
mPublishType = publishType; mPublishType = publishType;
mPublishCount = publichCount; mPublishCount = publichCount;
@@ -130,12 +114,10 @@ public class PublishConfig implements Parcelable {
@Override @Override
public String toString() { public String toString() {
return "PublishConfig [mServiceName='" + mServiceName + "', mServiceSpecificInfo='" return "PublishConfig [mServiceName='" + mServiceName + ", mServiceSpecificInfo='" + (
+ (new String(mServiceSpecificInfo, 0, mServiceSpecificInfoLength)) (mServiceSpecificInfo == null) ? "null" : HexEncoding.encode(mServiceSpecificInfo))
+ "', mTxFilter=" + ", mTxFilter=" + (new TlvBufferUtils.TlvIterable(0, 1, mTxFilter)).toString()
+ (new TlvBufferUtils.TlvIterable(0, 1, mTxFilter, mTxFilterLength)).toString() + ", mRxFilter=" + (new TlvBufferUtils.TlvIterable(0, 1, mRxFilter)).toString()
+ ", mRxFilter="
+ (new TlvBufferUtils.TlvIterable(0, 1, mRxFilter, mRxFilterLength)).toString()
+ ", mPublishType=" + mPublishType + ", mPublishCount=" + mPublishCount + ", mPublishType=" + mPublishType + ", mPublishCount=" + mPublishCount
+ ", mTtlSec=" + mTtlSec + ", mEnableTerminateNotification=" + ", mTtlSec=" + mTtlSec + ", mEnableTerminateNotification="
+ mEnableTerminateNotification + "]"; + mEnableTerminateNotification + "]";
@@ -148,22 +130,10 @@ public class PublishConfig implements Parcelable {
@Override @Override
public void writeToParcel(Parcel dest, int flags) { public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mServiceName.length); dest.writeByteArray(mServiceName);
if (mServiceName.length != 0) { dest.writeByteArray(mServiceSpecificInfo);
dest.writeByteArray(mServiceName); dest.writeByteArray(mTxFilter);
} dest.writeByteArray(mRxFilter);
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.writeInt(mPublishType); dest.writeInt(mPublishType);
dest.writeInt(mPublishCount); dest.writeInt(mPublishCount);
dest.writeInt(mTtlSec); dest.writeInt(mTtlSec);
@@ -178,34 +148,17 @@ public class PublishConfig implements Parcelable {
@Override @Override
public PublishConfig createFromParcel(Parcel in) { public PublishConfig createFromParcel(Parcel in) {
int serviceNameLength = in.readInt(); byte[] serviceName = in.createByteArray();
byte[] serviceName = new byte[serviceNameLength]; byte[] ssi = in.createByteArray();
if (serviceNameLength != 0) { byte[] txFilter = in.createByteArray();
in.readByteArray(serviceName); byte[] rxFilter = in.createByteArray();
}
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);
}
int publishType = in.readInt(); int publishType = in.readInt();
int publishCount = in.readInt(); int publishCount = in.readInt();
int ttlSec = in.readInt(); int ttlSec = in.readInt();
boolean enableTerminateNotification = in.readInt() != 0; boolean enableTerminateNotification = in.readInt() != 0;
return new PublishConfig(serviceName, ssi, ssiLength, txFilter, txFilterLength, return new PublishConfig(serviceName, ssi, txFilter, rxFilter, publishType,
rxFilter, rxFilterLength, publishType, publishCount, ttlSec, publishCount, ttlSec, enableTerminateNotification);
enableTerminateNotification);
} }
}; };
@@ -221,45 +174,10 @@ public class PublishConfig implements Parcelable {
PublishConfig lhs = (PublishConfig) o; PublishConfig lhs = (PublishConfig) o;
if (!Arrays.equals(mServiceName, lhs.mServiceName) return Arrays.equals(mServiceName, lhs.mServiceName) && Arrays.equals(mServiceSpecificInfo,
|| mServiceSpecificInfoLength != lhs.mServiceSpecificInfoLength lhs.mServiceSpecificInfo) && Arrays.equals(mTxFilter, lhs.mTxFilter)
|| mTxFilterLength != lhs.mTxFilterLength && Arrays.equals(mRxFilter, lhs.mRxFilter) && mPublishType == lhs.mPublishType
|| mRxFilterLength != lhs.mRxFilterLength) { && mPublishCount == lhs.mPublishCount && mTtlSec == lhs.mTtlSec
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
&& mEnableTerminateNotification == lhs.mEnableTerminateNotification; && mEnableTerminateNotification == lhs.mEnableTerminateNotification;
} }
@@ -268,11 +186,8 @@ public class PublishConfig implements Parcelable {
int result = 17; int result = 17;
result = 31 * result + Arrays.hashCode(mServiceName); result = 31 * result + Arrays.hashCode(mServiceName);
result = 31 * result + mServiceSpecificInfoLength;
result = 31 * result + Arrays.hashCode(mServiceSpecificInfo); result = 31 * result + Arrays.hashCode(mServiceSpecificInfo);
result = 31 * result + mTxFilterLength;
result = 31 * result + Arrays.hashCode(mTxFilter); result = 31 * result + Arrays.hashCode(mTxFilter);
result = 31 * result + mRxFilterLength;
result = 31 * result + Arrays.hashCode(mRxFilter); result = 31 * result + Arrays.hashCode(mRxFilter);
result = 31 * result + mPublishType; result = 31 * result + mPublishType;
result = 31 * result + mPublishCount; result = 31 * result + mPublishCount;
@@ -291,24 +206,11 @@ public class PublishConfig implements Parcelable {
public void validate() throws IllegalArgumentException { public void validate() throws IllegalArgumentException {
WifiNanUtils.validateServiceName(mServiceName); WifiNanUtils.validateServiceName(mServiceName);
if (mServiceSpecificInfoLength != 0 && (mServiceSpecificInfo == null if (!TlvBufferUtils.isValid(mTxFilter, 0, 1)) {
|| 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)) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Invalid txFilter configuration - LV fields do not match up to length"); "Invalid txFilter configuration - LV fields do not match up to length");
} }
if (mRxFilterLength != 0 && (mRxFilter == null || mRxFilter.length < mRxFilterLength)) { if (!TlvBufferUtils.isValid(mRxFilter, 0, 1)) {
throw new IllegalArgumentException(
"Non-matching combination of rxFilter and rxFilterLength");
}
if (!TlvBufferUtils.isValid(mRxFilter, mRxFilterLength, 0, 1)) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Invalid rxFilter configuration - LV fields do not match up to length"); "Invalid rxFilter configuration - LV fields do not match up to length");
} }
@@ -321,11 +223,13 @@ public class PublishConfig implements Parcelable {
if (mTtlSec < 0) { if (mTtlSec < 0) {
throw new IllegalArgumentException("Invalid ttlSec - must be non-negative"); 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 " throw new IllegalArgumentException("Invalid publish config: UNSOLICITED "
+ "publishes (active) can't have an Rx filter"); + "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 " throw new IllegalArgumentException("Invalid publish config: SOLICITED "
+ "publishes (passive) can't have a Tx filter"); + "publishes (passive) can't have a Tx filter");
} }
@@ -336,12 +240,9 @@ public class PublishConfig implements Parcelable {
*/ */
public static final class Builder { public static final class Builder {
private byte[] mServiceName; private byte[] mServiceName;
private int mServiceSpecificInfoLength; private byte[] mServiceSpecificInfo;
private byte[] mServiceSpecificInfo = new byte[0]; private byte[] mTxFilter;
private int mTxFilterLength; private byte[] mRxFilter;
private byte[] mTxFilter = new byte[0];
private int mRxFilterLength;
private byte[] mRxFilter = new byte[0];
private int mPublishType = PUBLISH_TYPE_UNSOLICITED; private int mPublishType = PUBLISH_TYPE_UNSOLICITED;
private int mPublishCount = 0; private int mPublishCount = 0;
private int mTtlSec = 0; private int mTtlSec = 0;
@@ -377,26 +278,17 @@ public class PublishConfig implements Parcelable {
* *
* @param serviceSpecificInfo A byte-array for the service-specific * @param serviceSpecificInfo A byte-array for the service-specific
* information field. * information field.
* @param serviceSpecificInfoLength The length of the byte-array to be
* used.
* @return The builder to facilitate chaining * @return The builder to facilitate chaining
* {@code builder.setXXX(..).setXXX(..)}. * {@code builder.setXXX(..).setXXX(..)}.
*/ */
public Builder setServiceSpecificInfo(byte[] serviceSpecificInfo, public Builder setServiceSpecificInfo(@Nullable byte[] serviceSpecificInfo) {
int serviceSpecificInfoLength) {
if (serviceSpecificInfoLength != 0 && (serviceSpecificInfo == null
|| serviceSpecificInfo.length < serviceSpecificInfoLength)) {
throw new IllegalArgumentException("Non-matching combination of "
+ "serviceSpecificInfo and serviceSpecificInfoLength");
}
mServiceSpecificInfoLength = serviceSpecificInfoLength;
mServiceSpecificInfo = serviceSpecificInfo; mServiceSpecificInfo = serviceSpecificInfo;
return this; return this;
} }
/** /**
* Specify service specific information for the publish session - same * 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. * but obtaining the data from a String.
* *
* @param serviceSpecificInfoStr The service specific information string * @param serviceSpecificInfoStr The service specific information string
@@ -407,7 +299,6 @@ public class PublishConfig implements Parcelable {
*/ */
public Builder setServiceSpecificInfo(@NonNull String serviceSpecificInfoStr) { public Builder setServiceSpecificInfo(@NonNull String serviceSpecificInfoStr) {
mServiceSpecificInfo = serviceSpecificInfoStr.getBytes(); mServiceSpecificInfo = serviceSpecificInfoStr.getBytes();
mServiceSpecificInfoLength = mServiceSpecificInfo.length;
return this; return this;
} }
@@ -424,18 +315,11 @@ public class PublishConfig implements Parcelable {
* *
* @param txFilter The byte-array containing the LV formatted transmit * @param txFilter The byte-array containing the LV formatted transmit
* filter. * filter.
* @param txFilterLength The number of bytes in the transmit filter
* argument.
* @return The builder to facilitate chaining * @return The builder to facilitate chaining
* {@code builder.setXXX(..).setXXX(..)}. * {@code builder.setXXX(..).setXXX(..)}.
*/ */
public Builder setTxFilter(byte[] txFilter, int txFilterLength) { public Builder setTxFilter(@Nullable byte[] txFilter) {
if (txFilterLength != 0 && (txFilter == null || txFilter.length < txFilterLength)) {
throw new IllegalArgumentException(
"Non-matching combination of txFilter and txFilterLength");
}
mTxFilter = txFilter; mTxFilter = txFilter;
mTxFilterLength = txFilterLength;
return this; return this;
} }
@@ -452,18 +336,11 @@ public class PublishConfig implements Parcelable {
* *
* @param rxFilter The byte-array containing the LV formatted receive * @param rxFilter The byte-array containing the LV formatted receive
* filter. * filter.
* @param rxFilterLength The number of bytes in the receive filter
* argument.
* @return The builder to facilitate chaining * @return The builder to facilitate chaining
* {@code builder.setXXX(..).setXXX(..)}. * {@code builder.setXXX(..).setXXX(..)}.
*/ */
public Builder setRxFilter(byte[] rxFilter, int rxFilterLength) { public Builder setRxFilter(@Nullable byte[] rxFilter) {
if (rxFilterLength != 0 && (rxFilter == null || rxFilter.length < rxFilterLength)) {
throw new IllegalArgumentException(
"Non-matching combination of rxFilter and rxFilterLength");
}
mRxFilter = rxFilter; mRxFilter = rxFilter;
mRxFilterLength = rxFilterLength;
return this; return this;
} }
@@ -547,9 +424,8 @@ public class PublishConfig implements Parcelable {
* builder. * builder.
*/ */
public PublishConfig build() { public PublishConfig build() {
return new PublishConfig(mServiceName, mServiceSpecificInfo, mServiceSpecificInfoLength, return new PublishConfig(mServiceName, mServiceSpecificInfo, mTxFilter, mRxFilter,
mTxFilter, mTxFilterLength, mRxFilter, mRxFilterLength, mPublishType, mPublishType, mPublishCount, mTtlSec, mEnableTerminateNotification);
mPublishCount, mTtlSec, mEnableTerminateNotification);
} }
} }
} }

View File

@@ -18,9 +18,12 @@ package android.net.wifi.nan;
import android.annotation.IntDef; import android.annotation.IntDef;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import libcore.util.HexEncoding;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@@ -79,31 +82,16 @@ public class SubscribeConfig implements Parcelable {
*/ */
public final byte[] mServiceName; public final byte[] mServiceName;
/**
* @hide
*/
public final int mServiceSpecificInfoLength;
/** /**
* @hide * @hide
*/ */
public final byte[] mServiceSpecificInfo; public final byte[] mServiceSpecificInfo;
/**
* @hide
*/
public final int mTxFilterLength;
/** /**
* @hide * @hide
*/ */
public final byte[] mTxFilter; public final byte[] mTxFilter;
/**
* @hide
*/
public final int mRxFilterLength;
/** /**
* @hide * @hide
*/ */
@@ -134,16 +122,12 @@ public class SubscribeConfig implements Parcelable {
*/ */
public final boolean mEnableTerminateNotification; public final boolean mEnableTerminateNotification;
private SubscribeConfig(byte[] serviceName, byte[] serviceSpecificInfo, private SubscribeConfig(byte[] serviceName, byte[] serviceSpecificInfo, byte[] txFilter,
int serviceSpecificInfoLength, byte[] txFilter, int txFilterLength, byte[] rxFilter, byte[] rxFilter, int subscribeType, int publichCount, int ttlSec, int matchStyle,
int rxFilterLength, int subscribeType, int publichCount, int ttlSec, int matchStyle,
boolean enableTerminateNotification) { boolean enableTerminateNotification) {
mServiceName = serviceName; mServiceName = serviceName;
mServiceSpecificInfoLength = serviceSpecificInfoLength;
mServiceSpecificInfo = serviceSpecificInfo; mServiceSpecificInfo = serviceSpecificInfo;
mTxFilterLength = txFilterLength;
mTxFilter = txFilter; mTxFilter = txFilter;
mRxFilterLength = rxFilterLength;
mRxFilter = rxFilter; mRxFilter = rxFilter;
mSubscribeType = subscribeType; mSubscribeType = subscribeType;
mSubscribeCount = publichCount; mSubscribeCount = publichCount;
@@ -154,12 +138,10 @@ public class SubscribeConfig implements Parcelable {
@Override @Override
public String toString() { public String toString() {
return "SubscribeConfig [mServiceName='" + mServiceName + "', mServiceSpecificInfo='" return "SubscribeConfig [mServiceName='" + mServiceName + ", mServiceSpecificInfo='" + (
+ (new String(mServiceSpecificInfo, 0, mServiceSpecificInfoLength)) (mServiceSpecificInfo == null) ? "null" : HexEncoding.encode(mServiceSpecificInfo))
+ "', mTxFilter=" + ", mTxFilter=" + (new TlvBufferUtils.TlvIterable(0, 1, mTxFilter)).toString()
+ (new TlvBufferUtils.TlvIterable(0, 1, mTxFilter, mTxFilterLength)).toString() + ", mRxFilter=" + (new TlvBufferUtils.TlvIterable(0, 1, mRxFilter)).toString()
+ ", mRxFilter="
+ (new TlvBufferUtils.TlvIterable(0, 1, mRxFilter, mRxFilterLength)).toString()
+ ", mSubscribeType=" + mSubscribeType + ", mSubscribeCount=" + mSubscribeCount + ", mSubscribeType=" + mSubscribeType + ", mSubscribeCount=" + mSubscribeCount
+ ", mTtlSec=" + mTtlSec + ", mMatchType=" + mMatchStyle + ", mTtlSec=" + mTtlSec + ", mMatchType=" + mMatchStyle
+ ", mEnableTerminateNotification=" + mEnableTerminateNotification + "]"; + ", mEnableTerminateNotification=" + mEnableTerminateNotification + "]";
@@ -172,22 +154,10 @@ public class SubscribeConfig implements Parcelable {
@Override @Override
public void writeToParcel(Parcel dest, int flags) { public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mServiceName.length); dest.writeByteArray(mServiceName);
if (mServiceName.length != 0) { dest.writeByteArray(mServiceSpecificInfo);
dest.writeByteArray(mServiceName); dest.writeByteArray(mTxFilter);
} dest.writeByteArray(mRxFilter);
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.writeInt(mSubscribeType); dest.writeInt(mSubscribeType);
dest.writeInt(mSubscribeCount); dest.writeInt(mSubscribeCount);
dest.writeInt(mTtlSec); dest.writeInt(mTtlSec);
@@ -203,35 +173,18 @@ public class SubscribeConfig implements Parcelable {
@Override @Override
public SubscribeConfig createFromParcel(Parcel in) { public SubscribeConfig createFromParcel(Parcel in) {
int serviceNameLength = in.readInt(); byte[] serviceName = in.createByteArray();
byte[] serviceName = new byte[serviceNameLength]; byte[] ssi = in.createByteArray();
if (serviceNameLength != 0) { byte[] txFilter = in.createByteArray();
in.readByteArray(serviceName); byte[] rxFilter = in.createByteArray();
}
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);
}
int subscribeType = in.readInt(); int subscribeType = in.readInt();
int subscribeCount = in.readInt(); int subscribeCount = in.readInt();
int ttlSec = in.readInt(); int ttlSec = in.readInt();
int matchStyle = in.readInt(); int matchStyle = in.readInt();
boolean enableTerminateNotification = in.readInt() != 0; boolean enableTerminateNotification = in.readInt() != 0;
return new SubscribeConfig(serviceName, ssi, ssiLength, txFilter, txFilterLength, return new SubscribeConfig(serviceName, ssi, txFilter, rxFilter, subscribeType,
rxFilter, rxFilterLength, subscribeType, subscribeCount, ttlSec, matchStyle, subscribeCount, ttlSec, matchStyle, enableTerminateNotification);
enableTerminateNotification);
} }
}; };
@@ -247,45 +200,11 @@ public class SubscribeConfig implements Parcelable {
SubscribeConfig lhs = (SubscribeConfig) o; SubscribeConfig lhs = (SubscribeConfig) o;
if (!Arrays.equals(mServiceName, lhs.mServiceName) return Arrays.equals(mServiceName, lhs.mServiceName) && Arrays.equals(mServiceSpecificInfo,
|| mServiceSpecificInfoLength != lhs.mServiceSpecificInfoLength lhs.mServiceSpecificInfo) && Arrays.equals(mTxFilter, lhs.mTxFilter)
|| mTxFilterLength != lhs.mTxFilterLength && Arrays.equals(mRxFilter, lhs.mRxFilter) && mSubscribeType == lhs.mSubscribeType
|| mRxFilterLength != lhs.mRxFilterLength) { && mSubscribeCount == lhs.mSubscribeCount && mTtlSec == lhs.mTtlSec
return false; && mMatchStyle == lhs.mMatchStyle
}
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 mSubscribeType == lhs.mSubscribeType && mSubscribeCount == lhs.mSubscribeCount
&& mTtlSec == lhs.mTtlSec && mMatchStyle == lhs.mMatchStyle
&& mEnableTerminateNotification == lhs.mEnableTerminateNotification; && mEnableTerminateNotification == lhs.mEnableTerminateNotification;
} }
@@ -294,11 +213,8 @@ public class SubscribeConfig implements Parcelable {
int result = 17; int result = 17;
result = 31 * result + Arrays.hashCode(mServiceName); result = 31 * result + Arrays.hashCode(mServiceName);
result = 31 * result + mServiceSpecificInfoLength;
result = 31 * result + Arrays.hashCode(mServiceSpecificInfo); result = 31 * result + Arrays.hashCode(mServiceSpecificInfo);
result = 31 * result + mTxFilterLength;
result = 31 * result + Arrays.hashCode(mTxFilter); result = 31 * result + Arrays.hashCode(mTxFilter);
result = 31 * result + mRxFilterLength;
result = 31 * result + Arrays.hashCode(mRxFilter); result = 31 * result + Arrays.hashCode(mRxFilter);
result = 31 * result + mSubscribeType; result = 31 * result + mSubscribeType;
result = 31 * result + mSubscribeCount; result = 31 * result + mSubscribeCount;
@@ -318,24 +234,11 @@ public class SubscribeConfig implements Parcelable {
public void validate() throws IllegalArgumentException { public void validate() throws IllegalArgumentException {
WifiNanUtils.validateServiceName(mServiceName); WifiNanUtils.validateServiceName(mServiceName);
if (mServiceSpecificInfoLength != 0 && (mServiceSpecificInfo == null if (!TlvBufferUtils.isValid(mTxFilter, 0, 1)) {
|| 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)) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Invalid txFilter configuration - LV fields do not match up to length"); "Invalid txFilter configuration - LV fields do not match up to length");
} }
if (mRxFilterLength != 0 && (mRxFilter == null || mRxFilter.length < mRxFilterLength)) { if (!TlvBufferUtils.isValid(mRxFilter, 0, 1)) {
throw new IllegalArgumentException(
"Non-matching combination of rxFilter and rxFilterLength");
}
if (!TlvBufferUtils.isValid(mRxFilter, mRxFilterLength, 0, 1)) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Invalid rxFilter configuration - LV fields do not match up to length"); "Invalid rxFilter configuration - LV fields do not match up to length");
} }
@@ -352,11 +255,13 @@ public class SubscribeConfig implements Parcelable {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Invalid matchType - must be MATCH_FIRST_ONLY or MATCH_ALL"); "Invalid matchType - must be MATCH_FIRST_ONLY or MATCH_ALL");
} }
if (mSubscribeType == SubscribeConfig.SUBSCRIBE_TYPE_ACTIVE && mRxFilterLength != 0) { if (mSubscribeType == SubscribeConfig.SUBSCRIBE_TYPE_ACTIVE && mRxFilter != null
&& mRxFilter.length != 0) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Invalid subscribe config: ACTIVE subscribes can't have an Rx filter"); "Invalid subscribe config: ACTIVE subscribes can't have an Rx filter");
} }
if (mSubscribeType == SubscribeConfig.SUBSCRIBE_TYPE_PASSIVE && mTxFilterLength != 0) { if (mSubscribeType == SubscribeConfig.SUBSCRIBE_TYPE_PASSIVE && mTxFilter != null
&& mTxFilter.length != 0) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Invalid subscribe config: PASSIVE subscribes can't have a Tx filter"); "Invalid subscribe config: PASSIVE subscribes can't have a Tx filter");
} }
@@ -367,12 +272,9 @@ public class SubscribeConfig implements Parcelable {
*/ */
public static final class Builder { public static final class Builder {
private byte[] mServiceName; private byte[] mServiceName;
private int mServiceSpecificInfoLength; private byte[] mServiceSpecificInfo;
private byte[] mServiceSpecificInfo = new byte[0]; private byte[] mTxFilter;
private int mTxFilterLength; private byte[] mRxFilter;
private byte[] mTxFilter = new byte[0];
private int mRxFilterLength;
private byte[] mRxFilter = new byte[0];
private int mSubscribeType = SUBSCRIBE_TYPE_PASSIVE; private int mSubscribeType = SUBSCRIBE_TYPE_PASSIVE;
private int mSubscribeCount = 0; private int mSubscribeCount = 0;
private int mTtlSec = 0; private int mTtlSec = 0;
@@ -409,19 +311,10 @@ public class SubscribeConfig implements Parcelable {
* *
* @param serviceSpecificInfo A byte-array for the service-specific * @param serviceSpecificInfo A byte-array for the service-specific
* information field. * information field.
* @param serviceSpecificInfoLength The length of the byte-array to be
* used.
* @return The builder to facilitate chaining * @return The builder to facilitate chaining
* {@code builder.setXXX(..).setXXX(..)}. * {@code builder.setXXX(..).setXXX(..)}.
*/ */
public Builder setServiceSpecificInfo(byte[] serviceSpecificInfo, public Builder setServiceSpecificInfo(@Nullable byte[] serviceSpecificInfo) {
int serviceSpecificInfoLength) {
if (serviceSpecificInfoLength != 0 && (serviceSpecificInfo == null
|| serviceSpecificInfo.length < serviceSpecificInfoLength)) {
throw new IllegalArgumentException("Non-matching combination of "
+ "serviceSpecificInfo and serviceSpecificInfoLength");
}
mServiceSpecificInfoLength = serviceSpecificInfoLength;
mServiceSpecificInfo = serviceSpecificInfo; mServiceSpecificInfo = serviceSpecificInfo;
return this; return this;
} }
@@ -429,7 +322,7 @@ public class SubscribeConfig implements Parcelable {
/** /**
* Specify service specific information for the subscribe session - same * Specify service specific information for the subscribe session - same
* as * as
* {@link SubscribeConfig.Builder#setServiceSpecificInfo(byte[], int)} * {@link SubscribeConfig.Builder#setServiceSpecificInfo(byte[])}
* but obtaining the data from a String. * but obtaining the data from a String.
* *
* @param serviceSpecificInfoStr The service specific information string * @param serviceSpecificInfoStr The service specific information string
@@ -440,7 +333,6 @@ public class SubscribeConfig implements Parcelable {
*/ */
public Builder setServiceSpecificInfo(@NonNull String serviceSpecificInfoStr) { public Builder setServiceSpecificInfo(@NonNull String serviceSpecificInfoStr) {
mServiceSpecificInfo = serviceSpecificInfoStr.getBytes(); mServiceSpecificInfo = serviceSpecificInfoStr.getBytes();
mServiceSpecificInfoLength = mServiceSpecificInfo.length;
return this; return this;
} }
@@ -457,18 +349,11 @@ public class SubscribeConfig implements Parcelable {
* *
* @param txFilter The byte-array containing the LV formatted transmit * @param txFilter The byte-array containing the LV formatted transmit
* filter. * filter.
* @param txFilterLength The number of bytes in the transmit filter
* argument.
* @return The builder to facilitate chaining * @return The builder to facilitate chaining
* {@code builder.setXXX(..).setXXX(..)}. * {@code builder.setXXX(..).setXXX(..)}.
*/ */
public Builder setTxFilter(byte[] txFilter, int txFilterLength) { public Builder setTxFilter(@Nullable byte[] txFilter) {
if (txFilterLength != 0 && (txFilter == null || txFilter.length < txFilterLength)) {
throw new IllegalArgumentException(
"Non-matching combination of txFilter and txFilterLength");
}
mTxFilter = txFilter; mTxFilter = txFilter;
mTxFilterLength = txFilterLength;
return this; return this;
} }
@@ -484,18 +369,11 @@ public class SubscribeConfig implements Parcelable {
* *
* @param rxFilter The byte-array containing the LV formatted receive * @param rxFilter The byte-array containing the LV formatted receive
* filter. * filter.
* @param rxFilterLength The number of bytes in the receive filter
* argument.
* @return The builder to facilitate chaining * @return The builder to facilitate chaining
* {@code builder.setXXX(..).setXXX(..)}. * {@code builder.setXXX(..).setXXX(..)}.
*/ */
public Builder setRxFilter(byte[] rxFilter, int rxFilterLength) { public Builder setRxFilter(@Nullable byte[] rxFilter) {
if (rxFilterLength != 0 && (rxFilter == null || rxFilter.length < rxFilterLength)) {
throw new IllegalArgumentException(
"Non-matching combination of rxFilter and rxFilterLength");
}
mRxFilter = rxFilter; mRxFilter = rxFilter;
mRxFilterLength = rxFilterLength;
return this; return this;
} }
@@ -563,7 +441,7 @@ public class SubscribeConfig implements Parcelable {
* Sets the match style of the subscription - how are matches from a * Sets the match style of the subscription - how are matches from a
* single match session (corresponding to the same publish action on the * single match session (corresponding to the same publish action on the
* peer) reported to the host (using the * peer) reported to the host (using the
* {@link WifiNanSessionCallback#onMatch(int, byte[], int, byte[], int)} * {@link WifiNanSessionCallback#onMatch(int, byte[], byte[])}
* ). The options are: only report the first match and ignore the rest * ). The options are: only report the first match and ignore the rest
* {@link SubscribeConfig#MATCH_STYLE_FIRST_ONLY} or report every single * {@link SubscribeConfig#MATCH_STYLE_FIRST_ONLY} or report every single
* match {@link SubscribeConfig#MATCH_STYLE_ALL}. * match {@link SubscribeConfig#MATCH_STYLE_ALL}.
@@ -601,9 +479,8 @@ public class SubscribeConfig implements Parcelable {
* builder. * builder.
*/ */
public SubscribeConfig build() { public SubscribeConfig build() {
return new SubscribeConfig(mServiceName, mServiceSpecificInfo, return new SubscribeConfig(mServiceName, mServiceSpecificInfo, mTxFilter, mRxFilter,
mServiceSpecificInfoLength, mTxFilter, mTxFilterLength, mRxFilter, mSubscribeType, mSubscribeCount, mTtlSec, mMatchStyle,
mRxFilterLength, mSubscribeType, mSubscribeCount, mTtlSec, mMatchStyle,
mEnableTerminateNotification); mEnableTerminateNotification);
} }
} }

View File

@@ -16,10 +16,13 @@
package android.net.wifi.nan; package android.net.wifi.nan;
import android.annotation.Nullable;
import libcore.io.Memory; import libcore.io.Memory;
import java.nio.BufferOverflowException; import java.nio.BufferOverflowException;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
/** /**
@@ -50,8 +53,7 @@ public class TlvBufferUtils {
* Values are added to the structure using the {@code TlvConstructor.put*()} * Values are added to the structure using the {@code TlvConstructor.put*()}
* methods. * methods.
* <p> * <p>
* The final byte array is obtained using {@link TlvConstructor#getArray()} * The final byte array is obtained using {@link TlvConstructor#getArray()}.
* and {@link TlvConstructor#getActualLength()} methods.
*/ */
public static class TlvConstructor { public static class TlvConstructor {
private int mTypeSize; private int mTypeSize;
@@ -88,9 +90,9 @@ public class TlvBufferUtils {
* @return The constructor to facilitate chaining * @return The constructor to facilitate chaining
* {@code ctr.putXXX(..).putXXX(..)}. * {@code ctr.putXXX(..).putXXX(..)}.
*/ */
public TlvConstructor wrap(byte[] array) { public TlvConstructor wrap(@Nullable byte[] array) {
mArray = array; mArray = array;
mArrayLength = array.length; mArrayLength = (array == null) ? 0 : array.length;
return this; return this;
} }
@@ -137,10 +139,13 @@ public class TlvBufferUtils {
* @return The constructor to facilitate chaining * @return The constructor to facilitate chaining
* {@code ctr.putXXX(..).putXXX(..)}. * {@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); checkLength(length);
addHeader(type, length); addHeader(type, length);
System.arraycopy(array, offset, mArray, mPosition, length); if (length != 0) {
System.arraycopy(array, offset, mArray, mPosition, length);
}
mPosition += length; mPosition += length;
return this; return this;
} }
@@ -155,8 +160,8 @@ public class TlvBufferUtils {
* @return The constructor to facilitate chaining * @return The constructor to facilitate chaining
* {@code ctr.putXXX(..).putXXX(..)}. * {@code ctr.putXXX(..).putXXX(..)}.
*/ */
public TlvConstructor putByteArray(int type, byte[] array) { public TlvConstructor putByteArray(int type, @Nullable byte[] array) {
return putByteArray(type, array, 0, array.length); return putByteArray(type, array, 0, (array == null) ? 0 : array.length);
} }
/** /**
@@ -223,24 +228,25 @@ public class TlvBufferUtils {
* @return The constructor to facilitate chaining * @return The constructor to facilitate chaining
* {@code ctr.putXXX(..).putXXX(..)}. * {@code ctr.putXXX(..).putXXX(..)}.
*/ */
public TlvConstructor putString(int type, String data) { public TlvConstructor putString(int type, @Nullable String data) {
byte[] bytes = data.getBytes(); byte[] bytes = null;
return putByteArray(type, bytes, 0, bytes.length); 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 * Returns the constructed TLV formatted byte-array. This array is a copy of the wrapped
* returned array is the fully wrapped ( * or allocated array - truncated to just the significant bytes - i.e. those written into
* {@link TlvConstructor#wrap(byte[])}) or allocated ( * the (T)LV.
* {@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.
* *
* @return The byte array containing the TLV formatted structure. * @return The byte array containing the TLV formatted structure.
*/ */
public byte[] getArray() { 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. * @return The size of the TLV formatted portion of the byte array.
*/ */
public int getActualLength() { private int getActualLength() {
return mPosition; return mPosition;
} }
@@ -307,7 +313,7 @@ public class TlvBufferUtils {
*/ */
public int mOffset; 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; mType = type;
mLength = length; mLength = length;
mRefArray = refArray; mRefArray = refArray;
@@ -389,10 +395,8 @@ public class TlvBufferUtils {
* @param lengthSize Number of bytes sued for the Length (L) field. * @param lengthSize Number of bytes sued for the Length (L) field.
* Values values are 1 or 2 bytes. * Values values are 1 or 2 bytes.
* @param array The TLV formatted byte-array to parse. * @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) { if (typeSize < 0 || typeSize > 2 || lengthSize <= 0 || lengthSize > 2) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Invalid sizes - typeSize=" + typeSize + ", lengthSize=" + lengthSize); "Invalid sizes - typeSize=" + typeSize + ", lengthSize=" + lengthSize);
@@ -400,7 +404,7 @@ public class TlvBufferUtils {
mTypeSize = typeSize; mTypeSize = typeSize;
mLengthSize = lengthSize; mLengthSize = lengthSize;
mArray = array; 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). * fields correctly fill the specified length (and do not overshoot).
* *
* @param array The (T)LV array to verify. * @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 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. * @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). * @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) { if (typeSize < 0 || typeSize > 2) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Invalid arguments - typeSize must be 0, 1, or 2: typeSize=" + typeSize); "Invalid arguments - typeSize must be 0, 1, or 2: typeSize=" + typeSize);
@@ -508,14 +511,12 @@ public class TlvBufferUtils {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Invalid arguments - lengthSize must be 1 or 2: lengthSize=" + lengthSize); "Invalid arguments - lengthSize must be 1 or 2: lengthSize=" + lengthSize);
} }
if (length < 0 || length > array.length) { if (array == null) {
throw new IllegalArgumentException( return true;
"Invalid arguments - length must be non-negative and <= array.length: length="
+ length + ", array.length=" + array.length);
} }
int nextTlvIndex = 0; int nextTlvIndex = 0;
while (nextTlvIndex + typeSize + lengthSize <= length) { while (nextTlvIndex + typeSize + lengthSize <= array.length) {
nextTlvIndex += typeSize; nextTlvIndex += typeSize;
if (lengthSize == 1) { if (lengthSize == 1) {
nextTlvIndex += lengthSize + array[nextTlvIndex]; nextTlvIndex += lengthSize + array[nextTlvIndex];
@@ -525,6 +526,6 @@ public class TlvBufferUtils {
} }
} }
return nextTlvIndex == length; return nextTlvIndex == array.length;
} }
} }

View File

@@ -22,6 +22,8 @@ import android.annotation.Nullable;
import android.annotation.SdkConstant; import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType; import android.annotation.SdkConstant.SdkConstantType;
import android.annotation.SystemApi; import android.annotation.SystemApi;
import android.net.ConnectivityManager;
import android.net.NetworkRequest;
import android.net.wifi.RttManager; import android.net.wifi.RttManager;
import android.os.Binder; import android.os.Binder;
import android.os.Bundle; import android.os.Bundle;
@@ -205,15 +207,15 @@ public class WifiNanManager {
/** /**
* Data-path creation role is that of INITIATOR. Used in * Data-path creation role is that of INITIATOR. Used in
* {@link #createNetworkSpecifier(int, byte[], byte[], int)} and * {@link #createNetworkSpecifier(int, byte[], byte[])} and
* {@link WifiNanSession#createNetworkSpecifier(int, int, byte[], int)}. * {@link WifiNanSession#createNetworkSpecifier(int, int, byte[])}.
*/ */
public static final int WIFI_NAN_DATA_PATH_ROLE_INITIATOR = 0; public static final int WIFI_NAN_DATA_PATH_ROLE_INITIATOR = 0;
/** /**
* Data-path creation role is that of RESPONDER. Used in * Data-path creation role is that of RESPONDER. Used in
* {@link #createNetworkSpecifier(int, byte[], byte[], int)} and * {@link #createNetworkSpecifier(int, byte[], byte[])} and
* {@link WifiNanSession#createNetworkSpecifier(int, int, byte[], int)}. * {@link WifiNanSession#createNetworkSpecifier(int, int, byte[])}.
*/ */
public static final int WIFI_NAN_DATA_PATH_ROLE_RESPONDER = 1; public static final int WIFI_NAN_DATA_PATH_ROLE_RESPONDER = 1;
@@ -524,12 +526,11 @@ public class WifiNanManager {
/** /**
* {@hide} * {@hide}
*/ */
public void sendMessage(int sessionId, int peerId, byte[] message, int messageLength, public void sendMessage(int sessionId, int peerId, byte[] message, int messageId,
int messageId, int retryCount) { int retryCount) {
if (VDBG) { if (VDBG) {
Log.v(TAG, "sendMessage(): sessionId=" + sessionId + ", peerId=" + peerId Log.v(TAG, "sendMessage(): sessionId=" + sessionId + ", peerId=" + peerId
+ ", messageLength=" + messageLength + ", messageId=" + messageId + ", messageId=" + messageId + ", retryCount=" + retryCount);
+ ", retryCount=" + retryCount);
} }
int clientId; int clientId;
@@ -543,8 +544,7 @@ public class WifiNanManager {
} }
try { try {
mService.sendMessage(clientId, sessionId, peerId, message, messageLength, messageId, mService.sendMessage(clientId, sessionId, peerId, message, messageId, retryCount);
retryCount);
} catch (RemoteException e) { } catch (RemoteException e) {
e.rethrowAsRuntimeException(); e.rethrowAsRuntimeException();
} }
@@ -587,10 +587,10 @@ public class WifiNanManager {
* {@hide} * {@hide}
*/ */
public String createNetworkSpecifier(@DataPathRole int role, int sessionId, int peerId, public String createNetworkSpecifier(@DataPathRole int role, int sessionId, int peerId,
byte[] token, int tokenLength) { byte[] token) {
if (VDBG) { if (VDBG) {
Log.v(TAG, "createNetworkSpecifier: role=" + role + ", sessionId=" + sessionId Log.v(TAG, "createNetworkSpecifier: role=" + role + ", sessionId=" + sessionId
+ ", peerId=" + peerId + ", token=" + token + ", tokenLength=" + tokenLength); + ", peerId=" + peerId + ", token=" + token);
} }
int type; int type;
@@ -621,10 +621,6 @@ public class WifiNanManager {
+ "INITIATOR"); + "INITIATOR");
} }
} }
if (tokenLength != 0 && (token == null || token.length < tokenLength)) {
throw new IllegalArgumentException(
"Non-matching combination of token and tokenLength");
}
int clientId; int clientId;
synchronized (mLock) { synchronized (mLock) {
@@ -650,7 +646,7 @@ public class WifiNanManager {
} }
if (token != null) { if (token != null) {
json.put(NETWORK_SPECIFIER_KEY_TOKEN, json.put(NETWORK_SPECIFIER_KEY_TOKEN,
Base64.encodeToString(token, 0, tokenLength, Base64.DEFAULT)); Base64.encodeToString(token, 0, token.length, Base64.DEFAULT));
} }
} catch (JSONException e) { } catch (JSONException e) {
return ""; return "";
@@ -674,19 +670,15 @@ public class WifiNanManager {
* data-path setup process. On the RESPONDER a null token is permitted and * data-path setup process. On the RESPONDER a null token is permitted and
* matches any peer token - an empty token requires the peer token to be empty * matches any peer token - an empty token requires the peer token to be empty
* as well. * as well.
* @param tokenLength The number of significant (usable) bytes from the {@code token} parameter.
* @return A string to be used to construct * @return A string to be used to construct
* {@link android.net.NetworkRequest.Builder#setNetworkSpecifier(String)} to pass to {@link * {@link android.net.NetworkRequest.Builder#setNetworkSpecifier(String)} to pass to {@link
* android.net.ConnectivityManager#requestNetwork(NetworkRequest, * android.net.ConnectivityManager#requestNetwork(NetworkRequest, ConnectivityManager.NetworkCallback)}
* ConnectivityManager.NetworkCallback)} * [or other varieties of that API].
* [or other varierties of that API].
*/ */
public String createNetworkSpecifier(@DataPathRole int role, @Nullable byte[] peer, public String createNetworkSpecifier(@DataPathRole int role, @Nullable byte[] peer,
@Nullable byte[] token, int tokenLength) { @Nullable byte[] token) {
if (VDBG) { if (VDBG) {
Log.v(TAG, Log.v(TAG, "createNetworkSpecifier: role=" + role + ", token=" + token);
"createNetworkSpecifier: role=" + role + ", token=" + token + ", tokenLength="
+ tokenLength);
} }
int type; int type;
@@ -721,10 +713,6 @@ public class WifiNanManager {
"createNetworkSpecifier: Invalid peer MAC address"); "createNetworkSpecifier: Invalid peer MAC address");
} }
} }
if (tokenLength != 0 && (token == null || token.length < tokenLength)) {
throw new IllegalArgumentException(
"Non-matching combination of token and tokenLength");
}
int clientId; int clientId;
synchronized (mLock) { synchronized (mLock) {
@@ -749,7 +737,7 @@ public class WifiNanManager {
} }
if (token != null) { if (token != null) {
json.put(NETWORK_SPECIFIER_KEY_TOKEN, json.put(NETWORK_SPECIFIER_KEY_TOKEN,
Base64.encodeToString(token, 0, tokenLength, Base64.DEFAULT)); Base64.encodeToString(token, 0, token.length, Base64.DEFAULT));
} }
} catch (JSONException e) { } catch (JSONException e) {
return ""; return "";
@@ -932,7 +920,6 @@ public class WifiNanManager {
private static final int CALLBACK_MESSAGE_SEND_FAIL = 6; private static final int CALLBACK_MESSAGE_SEND_FAIL = 6;
private static final int CALLBACK_MESSAGE_RECEIVED = 7; private static final int CALLBACK_MESSAGE_RECEIVED = 7;
private static final String MESSAGE_BUNDLE_KEY_PEER_ID = "peer_id";
private static final String MESSAGE_BUNDLE_KEY_MESSAGE = "message"; private static final String MESSAGE_BUNDLE_KEY_MESSAGE = "message";
private static final String MESSAGE_BUNDLE_KEY_MESSAGE2 = "message2"; private static final String MESSAGE_BUNDLE_KEY_MESSAGE2 = "message2";
@@ -983,11 +970,9 @@ public class WifiNanManager {
break; break;
case CALLBACK_MATCH: case CALLBACK_MATCH:
mOriginalCallback.onMatch( mOriginalCallback.onMatch(
msg.getData().getInt(MESSAGE_BUNDLE_KEY_PEER_ID),
msg.getData().getByteArray(MESSAGE_BUNDLE_KEY_MESSAGE),
msg.arg1, msg.arg1,
msg.getData().getByteArray(MESSAGE_BUNDLE_KEY_MESSAGE2), msg.getData().getByteArray(MESSAGE_BUNDLE_KEY_MESSAGE),
msg.arg2); msg.getData().getByteArray(MESSAGE_BUNDLE_KEY_MESSAGE2));
break; break;
case CALLBACK_MESSAGE_SEND_SUCCESS: case CALLBACK_MESSAGE_SEND_SUCCESS:
mOriginalCallback.onMessageSendSuccess(msg.arg1); mOriginalCallback.onMessageSendSuccess(msg.arg1);
@@ -996,9 +981,7 @@ public class WifiNanManager {
mOriginalCallback.onMessageSendFail(msg.arg1, msg.arg2); mOriginalCallback.onMessageSendFail(msg.arg1, msg.arg2);
break; break;
case CALLBACK_MESSAGE_RECEIVED: case CALLBACK_MESSAGE_RECEIVED:
mOriginalCallback.onMessageReceived(msg.arg2, mOriginalCallback.onMessageReceived(msg.arg1, (byte[]) msg.obj);
msg.getData().getByteArray(MESSAGE_BUNDLE_KEY_MESSAGE),
msg.arg1);
break; break;
} }
} }
@@ -1041,18 +1024,15 @@ public class WifiNanManager {
} }
@Override @Override
public void onMatch(int peerId, byte[] serviceSpecificInfo, public void onMatch(int peerId, byte[] serviceSpecificInfo, byte[] matchFilter) {
int serviceSpecificInfoLength, byte[] matchFilter, int matchFilterLength) {
if (VDBG) Log.v(TAG, "onMatch: peerId=" + peerId); if (VDBG) Log.v(TAG, "onMatch: peerId=" + peerId);
Bundle data = new Bundle(); Bundle data = new Bundle();
data.putInt(MESSAGE_BUNDLE_KEY_PEER_ID, peerId);
data.putByteArray(MESSAGE_BUNDLE_KEY_MESSAGE, serviceSpecificInfo); data.putByteArray(MESSAGE_BUNDLE_KEY_MESSAGE, serviceSpecificInfo);
data.putByteArray(MESSAGE_BUNDLE_KEY_MESSAGE2, matchFilter); data.putByteArray(MESSAGE_BUNDLE_KEY_MESSAGE2, matchFilter);
Message msg = mHandler.obtainMessage(CALLBACK_MATCH); Message msg = mHandler.obtainMessage(CALLBACK_MATCH);
msg.arg1 = serviceSpecificInfoLength; msg.arg1 = peerId;
msg.arg2 = matchFilterLength;
msg.setData(data); msg.setData(data);
mHandler.sendMessage(msg); mHandler.sendMessage(msg);
} }
@@ -1077,19 +1057,14 @@ public class WifiNanManager {
} }
@Override @Override
public void onMessageReceived(int peerId, byte[] message, int messageLength) { public void onMessageReceived(int peerId, byte[] message) {
if (VDBG) { if (VDBG) {
Log.v(TAG, "onMessageReceived: peerId='" + peerId + "', messageLength=" Log.v(TAG, "onMessageReceived: peerId='" + peerId);
+ messageLength);
} }
Bundle data = new Bundle();
data.putByteArray(MESSAGE_BUNDLE_KEY_MESSAGE, message);
Message msg = mHandler.obtainMessage(CALLBACK_MESSAGE_RECEIVED); Message msg = mHandler.obtainMessage(CALLBACK_MESSAGE_RECEIVED);
msg.arg1 = messageLength; msg.arg1 = peerId;
msg.arg2 = peerId; msg.obj = message;
msg.setData(data);
mHandler.sendMessage(msg); mHandler.sendMessage(msg);
} }

View File

@@ -108,12 +108,11 @@ public class WifiNanSession {
/** /**
* Sends a message to the specified destination. Message transmission is part of the current * Sends a message to the specified destination. Message transmission is part of the current
* discovery session - i.e. executed subsequent to a publish/subscribe * discovery session - i.e. executed subsequent to a publish/subscribe
* {@link WifiNanSessionCallback#onMatch(int, byte[], int, byte[], int)} event. * {@link WifiNanSessionCallback#onMatch(int, byte[], byte[])} event.
* *
* @param peerId The peer's ID for the message. Must be a result of an * @param peerId The peer's ID for the message. Must be a result of an
* {@link WifiNanSessionCallback#onMatch(int, byte[], int, byte[], int)} event. * {@link WifiNanSessionCallback#onMatch(int, byte[], byte[])} event.
* @param message The message to be transmitted. * @param message The message to be transmitted.
* @param messageLength The number of bytes from the {@code message} to be transmitted.
* @param messageId An arbitrary integer used by the caller to identify the message. The same * @param messageId An arbitrary integer used by the caller to identify the message. The same
* integer ID will be returned in the callbacks indicated message send success or * integer ID will be returned in the callbacks indicated message send success or
* failure. * failure.
@@ -122,8 +121,7 @@ public class WifiNanSession {
* (note: no retransmissions are attempted in other failure cases). A value of 0 * (note: no retransmissions are attempted in other failure cases). A value of 0
* indicates no retries. Max possible value is {@link #MAX_SEND_RETRY_COUNT}. * indicates no retries. Max possible value is {@link #MAX_SEND_RETRY_COUNT}.
*/ */
public void sendMessage(int peerId, byte[] message, int messageLength, int messageId, public void sendMessage(int peerId, @Nullable byte[] message, int messageId, int retryCount) {
int retryCount) {
if (mTerminated) { if (mTerminated) {
Log.w(TAG, "sendMessage: called on terminated session"); Log.w(TAG, "sendMessage: called on terminated session");
return; return;
@@ -134,33 +132,32 @@ public class WifiNanSession {
return; return;
} }
mgr.sendMessage(mSessionId, peerId, message, messageLength, messageId, retryCount); mgr.sendMessage(mSessionId, peerId, message, messageId, retryCount);
} }
} }
/** /**
* Sends a message to the specified destination. Message transmission is part of the current * Sends a message to the specified destination. Message transmission is part of the current
* discovery session - i.e. executed subsequent to a publish/subscribe * discovery session - i.e. executed subsequent to a publish/subscribe
* {@link WifiNanSessionCallback#onMatch(int, byte[], int, byte[], int)} event. This is * {@link WifiNanSessionCallback#onMatch(int, byte[], byte[])} event. This is
* equivalent to {@link #sendMessage(int, byte[], int, int, int)} with a {@code retryCount} of * equivalent to {@link #sendMessage(int, byte[], int, int)} with a {@code retryCount} of
* 0. * 0.
* *
* @param peerId The peer's ID for the message. Must be a result of an * @param peerId The peer's ID for the message. Must be a result of an
* {@link WifiNanSessionCallback#onMatch(int, byte[], int, byte[], int)} event. * {@link WifiNanSessionCallback#onMatch(int, byte[], byte[])} event.
* @param message The message to be transmitted. * @param message The message to be transmitted.
* @param messageLength The number of bytes from the {@code message} to be transmitted.
* @param messageId An arbitrary integer used by the caller to identify the message. The same * @param messageId An arbitrary integer used by the caller to identify the message. The same
* integer ID will be returned in the callbacks indicated message send success or * integer ID will be returned in the callbacks indicated message send success or
* failure. * failure.
*/ */
public void sendMessage(int peerId, byte[] message, int messageLength, int messageId) { public void sendMessage(int peerId, @Nullable byte[] message, int messageId) {
sendMessage(peerId, message, messageLength, messageId, 0); sendMessage(peerId, message, messageId, 0);
} }
/** /**
* Start a ranging operation with the specified peers. The peer IDs are obtained from an * Start a ranging operation with the specified peers. The peer IDs are obtained from an
* {@link WifiNanSessionCallback#onMatch(int, byte[], int, byte[], int)} or * {@link WifiNanSessionCallback#onMatch(int, byte[], byte[])} or
* {@link WifiNanSessionCallback#onMessageReceived(int, byte[], int)} operation - i.e. can only * {@link WifiNanSessionCallback#onMessageReceived(int, byte[])} operation - i.e. can only
* range devices which are part of an ongoing discovery session. * range devices which are part of an ongoing discovery session.
* *
* @param params RTT parameters - each corresponding to a specific peer ID (the array sizes * @param params RTT parameters - each corresponding to a specific peer ID (the array sizes
@@ -194,22 +191,20 @@ public class WifiNanSession {
* {@link WifiNanManager#WIFI_NAN_DATA_PATH_ROLE_INITIATOR} or * {@link WifiNanManager#WIFI_NAN_DATA_PATH_ROLE_INITIATOR} or
* {@link WifiNanManager#WIFI_NAN_DATA_PATH_ROLE_RESPONDER} * {@link WifiNanManager#WIFI_NAN_DATA_PATH_ROLE_RESPONDER}
* @param peerId The peer ID obtained through * @param peerId The peer ID obtained through
* {@link WifiNanSessionCallback#onMatch(int, byte[], int, byte[], int)} or * {@link WifiNanSessionCallback#onMatch(int, byte[], byte[])} or
* {@link WifiNanSessionCallback#onMessageReceived(int, byte[], int)}. On the RESPONDER a * {@link WifiNanSessionCallback#onMessageReceived(int, byte[])}. On the RESPONDER a
* value of 0 is permitted which matches any peer. * value of 0 is permitted which matches any peer.
* @param token An arbitrary token (message) to be passed to the peer as part of the * @param token An arbitrary token (message) to be passed to the peer as part of the
* data-path setup process. On the RESPONDER a null token is permitted and * data-path setup process. On the RESPONDER a null token is permitted and
* matches any peer token - an empty token requires the peer token to be empty * matches any peer token - an empty token requires the peer token to be empty
* as well. * as well.
* @param tokenLength The number of significant (usable) bytes from the {@code token} parameter.
* @return A string to be used to construct * @return A string to be used to construct
* {@link android.net.NetworkRequest.Builder#setNetworkSpecifier(String)} to pass to {@link * {@link android.net.NetworkRequest.Builder#setNetworkSpecifier(String)} to pass to {@link
* android.net.ConnectivityManager#requestNetwork(NetworkRequest, * android.net.ConnectivityManager#requestNetwork(NetworkRequest,
* ConnectivityManager.NetworkCallback)} * ConnectivityManager.NetworkCallback)} [or other varieties of that API].
* [or other varierties of that API].
*/ */
public String createNetworkSpecifier(@WifiNanManager.DataPathRole int role, int peerId, public String createNetworkSpecifier(@WifiNanManager.DataPathRole int role, int peerId,
@Nullable byte[] token, int tokenLength) { @Nullable byte[] token) {
if (mTerminated) { if (mTerminated) {
Log.w(TAG, "createNetworkSpecifier: called on terminated session"); Log.w(TAG, "createNetworkSpecifier: called on terminated session");
return null; return null;
@@ -220,7 +215,7 @@ public class WifiNanSession {
return null; return null;
} }
return mgr.createNetworkSpecifier(role, mSessionId, peerId, token, tokenLength); return mgr.createNetworkSpecifier(role, mSessionId, peerId, token);
} }
} }
} }

View File

@@ -156,14 +156,10 @@ public class WifiNanSessionCallback {
* @param serviceSpecificInfo The service specific information (arbitrary * @param serviceSpecificInfo The service specific information (arbitrary
* byte array) provided by the peer as part of its discovery * byte array) provided by the peer as part of its discovery
* packet. * packet.
* @param serviceSpecificInfoLength The length of the service specific
* information array.
* @param matchFilter The filter (Tx on advertiser and Rx on listener) which * @param matchFilter The filter (Tx on advertiser and Rx on listener) which
* resulted in this match. * resulted in this match.
* @param matchFilterLength The length of the match filter array.
*/ */
public void onMatch(int peerId, byte[] serviceSpecificInfo, public void onMatch(int peerId, byte[] serviceSpecificInfo, byte[] matchFilter) {
int serviceSpecificInfoLength, byte[] matchFilter, int matchFilterLength) {
/* empty */ /* empty */
} }
@@ -204,10 +200,8 @@ public class WifiNanSessionCallback {
* *
* @param peerId The ID of the peer sending the message. * @param peerId The ID of the peer sending the message.
* @param message A byte array containing the message. * @param message A byte array containing the message.
* @param messageLength The length of the byte array containing the relevant
* message bytes.
*/ */
public void onMessageReceived(int peerId, byte[] message, int messageLength) { public void onMessageReceived(int peerId, byte[] message) {
/* empty */ /* empty */
} }
} }