Use IntRange for setPsm

Configures an IntRange [0, 255] for the L2CAP BLE PSM. Note that L2CAP
over Bluetooth Classic uses PSM values that exceed this range though is
explicitly not supported.

This CL also changes the value for PSM_ANY from -1 to 0 to make the
IntRange continuous as 0 is not a valid PSM (in both BLE and Classic).

Bug: 388438641
Test: TH
Change-Id: I15a69cfb80545c4f3723d089afd3cf026eec0696
This commit is contained in:
Patrick Rohr 2025-01-08 12:24:17 -08:00
commit 5b6e6b2a5a
2 changed files with 9 additions and 5 deletions

View file

@ -244,7 +244,7 @@ package android.net {
field public static final int HEADER_COMPRESSION_6LOWPAN = 2; // 0x2
field public static final int HEADER_COMPRESSION_ANY = 0; // 0x0
field public static final int HEADER_COMPRESSION_NONE = 1; // 0x1
field public static final int PSM_ANY = -1; // 0xffffffff
field public static final int PSM_ANY = 0; // 0x0
field public static final int ROLE_ANY = 0; // 0x0
field public static final int ROLE_CLIENT = 1; // 0x1
field public static final int ROLE_SERVER = 2; // 0x2
@ -254,7 +254,7 @@ package android.net {
ctor public L2capNetworkSpecifier.Builder();
method @NonNull public android.net.L2capNetworkSpecifier build();
method @NonNull public android.net.L2capNetworkSpecifier.Builder setHeaderCompression(int);
method @NonNull public android.net.L2capNetworkSpecifier.Builder setPsm(int);
method @NonNull public android.net.L2capNetworkSpecifier.Builder setPsm(@IntRange(from=0, to=255) int);
method @NonNull public android.net.L2capNetworkSpecifier.Builder setRemoteAddress(@Nullable android.net.MacAddress);
method @NonNull public android.net.L2capNetworkSpecifier.Builder setRole(int);
}

View file

@ -18,6 +18,7 @@ package android.net;
import android.annotation.FlaggedApi;
import android.annotation.IntDef;
import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.Build;
@ -31,7 +32,7 @@ import java.lang.annotation.RetentionPolicy;
import java.util.Objects;
/**
* A {@link NetworkSpecifier} used to identify an L2CAP network.
* A {@link NetworkSpecifier} used to identify an L2CAP network over BLE.
*
* An L2CAP network is not symmetrical, meaning there exists both a server (Bluetooth peripheral)
* and a client (Bluetooth central) node. This specifier contains the information required to
@ -119,7 +120,7 @@ public final class L2capNetworkSpecifier extends NetworkSpecifier implements Par
* This PSM value is only meaningful in {@link NetworkRequest}s. Specifiers for actual L2CAP
* networks never have this value set.
*/
public static final int PSM_ANY = -1;
public static final int PSM_ANY = 0;
/** The Bluetooth L2CAP Protocol/Service Multiplexer (PSM). */
private final int mPsm;
@ -228,7 +229,10 @@ public final class L2capNetworkSpecifier extends NetworkSpecifier implements Par
* @param psm the Protocol/Service Multiplexer (PSM) to connect to.
*/
@NonNull
public Builder setPsm(int psm) {
public Builder setPsm(@IntRange(from = 0, to = 255) int psm) {
if (psm < 0 /* PSM_ANY */ || psm > 0xFF) {
throw new IllegalArgumentException("PSM must be PSM_ANY or within range [1, 255]");
}
mPsm = psm;
return this;
}