From 5b6e6b2a5a983fd524b29cbfe12f94f16b5abe4a Mon Sep 17 00:00:00 2001 From: Patrick Rohr Date: Wed, 8 Jan 2025 12:24:17 -0800 Subject: [PATCH] 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 --- framework/api/current.txt | 4 ++-- framework/src/android/net/L2capNetworkSpecifier.java | 10 +++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/framework/api/current.txt b/framework/api/current.txt index a0801499ca..323c533ca0 100644 --- a/framework/api/current.txt +++ b/framework/api/current.txt @@ -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); } diff --git a/framework/src/android/net/L2capNetworkSpecifier.java b/framework/src/android/net/L2capNetworkSpecifier.java index 0d46a3e106..6770add828 100644 --- a/framework/src/android/net/L2capNetworkSpecifier.java +++ b/framework/src/android/net/L2capNetworkSpecifier.java @@ -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; }