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

Commit 424735e4 authored by Chienyuan Huang's avatar Chienyuan Huang
Browse files

Add class to set parameters for Channel Sounding

Bug: 319563845
Bug: 317683528
Test: atest ChannelSoundingParamsTest
Test: atest DistanceMeasurementParamsTest
Change-Id: I10e508d02a71e158b3306ebea3abf472abc6614c
parent 2817688b
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
@@ -1227,6 +1227,33 @@ package android.bluetooth.le {
    method @Deprecated @RequiresPermission(android.Manifest.permission.BLUETOOTH_SCAN) public void startTruncatedScan(java.util.List<android.bluetooth.le.TruncatedFilter>, android.bluetooth.le.ScanSettings, android.bluetooth.le.ScanCallback);
  }

  @FlaggedApi("com.android.bluetooth.flags.channel_sounding") public final class ChannelSoundingParams implements android.os.Parcelable {
    method public int describeContents();
    method public int getCsSecurityLevel();
    method public int getLocationType();
    method public int getSightType();
    method public void writeToParcel(@NonNull android.os.Parcel, int);
    field @NonNull public static final android.os.Parcelable.Creator<android.bluetooth.le.ChannelSoundingParams> CREATOR;
    field public static final int CS_SECURITY_LEVEL_FOUR = 4; // 0x4
    field public static final int CS_SECURITY_LEVEL_ONE = 1; // 0x1
    field public static final int CS_SECURITY_LEVEL_THREE = 3; // 0x3
    field public static final int CS_SECURITY_LEVEL_TWO = 2; // 0x2
    field public static final int LOCATION_TYPE_INDOOR = 1; // 0x1
    field public static final int LOCATION_TYPE_OUTDOOR = 2; // 0x2
    field public static final int LOCATION_TYPE_UNKNOWN = 0; // 0x0
    field public static final int SIGHT_TYPE_LINE_OF_SIGHT = 1; // 0x1
    field public static final int SIGHT_TYPE_NON_LINE_OF_SIGHT = 2; // 0x2
    field public static final int SIGHT_TYPE_UNKNOWN = 0; // 0x0
  }

  public static final class ChannelSoundingParams.Builder {
    ctor public ChannelSoundingParams.Builder();
    method @NonNull public android.bluetooth.le.ChannelSoundingParams build();
    method @NonNull public android.bluetooth.le.ChannelSoundingParams.Builder setCsSecurityLevel(int);
    method @NonNull public android.bluetooth.le.ChannelSoundingParams.Builder setLocationType(int);
    method @NonNull public android.bluetooth.le.ChannelSoundingParams.Builder setSightType(int);
  }

  public final class DistanceMeasurementManager {
    method @NonNull @RequiresPermission(allOf={android.Manifest.permission.BLUETOOTH_CONNECT, android.Manifest.permission.BLUETOOTH_PRIVILEGED}) public java.util.List<android.bluetooth.le.DistanceMeasurementMethod> getSupportedMethods();
    method @Nullable @RequiresPermission(allOf={android.Manifest.permission.BLUETOOTH_CONNECT, android.Manifest.permission.BLUETOOTH_PRIVILEGED}) public android.os.CancellationSignal startMeasurementSession(@NonNull android.bluetooth.le.DistanceMeasurementParams, @NonNull java.util.concurrent.Executor, @NonNull android.bluetooth.le.DistanceMeasurementSession.Callback);
@@ -1253,6 +1280,7 @@ package android.bluetooth.le {

  public final class DistanceMeasurementParams implements android.os.Parcelable {
    method public int describeContents();
    method @FlaggedApi("com.android.bluetooth.flags.channel_sounding") @Nullable public android.bluetooth.le.ChannelSoundingParams getChannelSoundingParams();
    method public static int getDefaultDurationSeconds();
    method @NonNull public android.bluetooth.BluetoothDevice getDevice();
    method @IntRange(from=0) public int getDurationSeconds();
@@ -1269,6 +1297,7 @@ package android.bluetooth.le {
  public static final class DistanceMeasurementParams.Builder {
    ctor public DistanceMeasurementParams.Builder(@NonNull android.bluetooth.BluetoothDevice);
    method @NonNull public android.bluetooth.le.DistanceMeasurementParams build();
    method @FlaggedApi("com.android.bluetooth.flags.channel_sounding") @NonNull public android.bluetooth.le.DistanceMeasurementParams.Builder setChannelSoundingParams(@NonNull android.bluetooth.le.ChannelSoundingParams);
    method @NonNull public android.bluetooth.le.DistanceMeasurementParams.Builder setDurationSeconds(@IntRange(from=0) int);
    method @NonNull public android.bluetooth.le.DistanceMeasurementParams.Builder setFrequency(int);
    method @NonNull public android.bluetooth.le.DistanceMeasurementParams.Builder setMethodId(int);
+309 −0
Original line number Diff line number Diff line
/*
 * Copyright 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.bluetooth.le;

import android.annotation.FlaggedApi;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.os.Parcel;
import android.os.Parcelable;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * The {@link ChannelSoundingParams} provide a way to adjust distance measurement preferences for
 * {@link DISTANCE_MEASUREMENT_METHOD_CHANNEL_SOUNDING}. Use {@link ChannelSoundingParams.Builder}
 * to create an instance of this class.
 *
 * @hide
 */
@FlaggedApi("com.android.bluetooth.flags.channel_sounding")
@SystemApi
public final class ChannelSoundingParams implements Parcelable {

    /** @hide */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef(
            value = {
                SIGHT_TYPE_UNKNOWN,
                SIGHT_TYPE_LINE_OF_SIGHT,
                SIGHT_TYPE_NON_LINE_OF_SIGHT,
            })
    @interface SightType {}

    /** @hide */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef(value = {LOCATION_TYPE_UNKNOWN, LOCATION_TYPE_INDOOR, LOCATION_TYPE_OUTDOOR})
    @interface LocationType {}

    /** @hide */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef(
            value = {
                CS_SECURITY_LEVEL_ONE,
                CS_SECURITY_LEVEL_TWO,
                CS_SECURITY_LEVEL_THREE,
                CS_SECURITY_LEVEL_FOUR
            })
    @interface CsSecurityLevel {}

    /**
     * Sight type is unknown.
     *
     * @hide
     */
    @SystemApi public static final int SIGHT_TYPE_UNKNOWN = 0;

    /**
     * Remote device is in line of sight.
     *
     * @hide
     */
    @SystemApi public static final int SIGHT_TYPE_LINE_OF_SIGHT = 1;

    /**
     * Remote device is not in line of sight.
     *
     * @hide
     */
    @SystemApi public static final int SIGHT_TYPE_NON_LINE_OF_SIGHT = 2;

    /**
     * Location type is unknown.
     *
     * @hide
     */
    @SystemApi public static final int LOCATION_TYPE_UNKNOWN = 0;

    /**
     * The location of the usecase is indoor.
     *
     * @hide
     */
    @SystemApi public static final int LOCATION_TYPE_INDOOR = 1;

    /**
     * The location of the usecase is outdoor.
     *
     * @hide
     */
    @SystemApi public static final int LOCATION_TYPE_OUTDOOR = 2;

    /**
     * Either CS tone or CS RTT.
     *
     * @hide
     */
    @SystemApi public static final int CS_SECURITY_LEVEL_ONE = 1;

    /**
     * 150 ns CS RTT accuracy and CS tones.
     *
     * @hide
     */
    @SystemApi public static final int CS_SECURITY_LEVEL_TWO = 2;

    /**
     * 10 ns CS RTT accuracy and CS tones.
     *
     * @hide
     */
    @SystemApi public static final int CS_SECURITY_LEVEL_THREE = 3;

    /**
     * Level 3 with the addition of CS RTT sounding sequence or random sequence payloads, and
     * support of the Normalized Attack Detector Metric requirements.
     *
     * @hide
     */
    @SystemApi public static final int CS_SECURITY_LEVEL_FOUR = 4;

    private int mSightType;
    private int mLocationType;
    private int mCsSecurityLevel;

    /** @hide */
    public ChannelSoundingParams(int sightType, int locationType, int csSecurityLevel) {
        mSightType = sightType;
        mLocationType = locationType;
        mCsSecurityLevel = csSecurityLevel;
    }

    /**
     * Returns sight type of this ChannelSoundingParams.
     *
     * @hide
     */
    @SystemApi
    @SightType
    public int getSightType() {
        return mSightType;
    }

    /**
     * Returns location type of this ChannelSoundingParams.
     *
     * @hide
     */
    @SystemApi
    @LocationType
    public int getLocationType() {
        return mLocationType;
    }

    /**
     * Returns CS security level of this ChannelSoundingParams.
     *
     * @hide
     */
    @SystemApi
    @CsSecurityLevel
    public int getCsSecurityLevel() {
        return mCsSecurityLevel;
    }

    /**
     * {@inheritDoc}
     *
     * @hide
     */
    @Override
    public int describeContents() {
        return 0;
    }

    /**
     * {@inheritDoc}
     *
     * @hide
     */
    @Override
    public void writeToParcel(@NonNull Parcel out, int flags) {
        out.writeInt(mSightType);
        out.writeInt(mLocationType);
        out.writeInt(mCsSecurityLevel);
    }

    /** A {@link Parcelable.Creator} to create {@link ChannelSoundingParams} from parcel. */
    public static final @NonNull Parcelable.Creator<ChannelSoundingParams> CREATOR =
            new Parcelable.Creator<ChannelSoundingParams>() {
                @Override
                public @NonNull ChannelSoundingParams createFromParcel(@NonNull Parcel in) {
                    Builder builder = new Builder();
                    builder.setSightType(in.readInt());
                    builder.setLocationType(in.readInt());
                    builder.setCsSecurityLevel(in.readInt());
                    return builder.build();
                }

                @Override
                public @NonNull ChannelSoundingParams[] newArray(int size) {
                    return new ChannelSoundingParams[size];
                }
            };

    /**
     * Builder for {@link ChannelSoundingParams}.
     *
     * @hide
     */
    @SystemApi
    public static final class Builder {
        private int mSightType = SIGHT_TYPE_UNKNOWN;
        private int mLocationType = LOCATION_TYPE_UNKNOWN;
        private int mCsSecurityLevel = CS_SECURITY_LEVEL_ONE;

        /**
         * Set sight type for the ChannelSoundingParams.
         *
         * @param sightType sight type of this ChannelSoundingParams
         * @return the same Builder instance
         * @hide
         */
        @SystemApi
        public @NonNull Builder setSightType(@SightType int sightType) {
            switch (sightType) {
                case SIGHT_TYPE_UNKNOWN:
                case SIGHT_TYPE_LINE_OF_SIGHT:
                case SIGHT_TYPE_NON_LINE_OF_SIGHT:
                    mSightType = sightType;
                    break;
                default:
                    throw new IllegalArgumentException("unknown sight type " + sightType);
            }
            return this;
        }

        /**
         * Set location type for the ChannelSoundingParams.
         *
         * @param locationType location type of this ChannelSoundingParams
         * @return the same Builder instance
         * @hide
         */
        @SystemApi
        public @NonNull Builder setLocationType(@LocationType int locationType) {
            switch (locationType) {
                case LOCATION_TYPE_UNKNOWN:
                case LOCATION_TYPE_INDOOR:
                case LOCATION_TYPE_OUTDOOR:
                    mLocationType = locationType;
                    break;
                default:
                    throw new IllegalArgumentException("unknown location type " + locationType);
            }
            return this;
        }

        /**
         * Set CS security level for the ChannelSoundingParams.
         *
         * <p>See: https://bluetooth.com/specifications/specs/channel-sounding-cr-pr/
         *
         * @param csSecurityLevel cs security level of this ChannelSoundingParams
         * @return the same Builder instance
         * @hide
         */
        @SystemApi
        public @NonNull Builder setCsSecurityLevel(@CsSecurityLevel int csSecurityLevel) {
            switch (csSecurityLevel) {
                case CS_SECURITY_LEVEL_ONE:
                case CS_SECURITY_LEVEL_TWO:
                case CS_SECURITY_LEVEL_THREE:
                case CS_SECURITY_LEVEL_FOUR:
                    mCsSecurityLevel = csSecurityLevel;
                    break;
                default:
                    throw new IllegalArgumentException(
                            "unknown CS security level " + csSecurityLevel);
            }
            return this;
        }

        /**
         * Build the {@link ChannelSoundingParams} object.
         *
         * @hide
         */
        @SystemApi
        public @NonNull ChannelSoundingParams build() {
            return new ChannelSoundingParams(mSightType, mLocationType, mCsSecurityLevel);
        }
    }
}
+41 −2
Original line number Diff line number Diff line
@@ -16,9 +16,11 @@

package android.bluetooth.le;

import android.annotation.FlaggedApi;
import android.annotation.IntDef;
import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.DistanceMeasurementMethod.DistanceMeasurementMethodId;
@@ -74,14 +76,20 @@ public final class DistanceMeasurementParams implements Parcelable {
    private int mDuration;
    private int mFrequency;
    private int mMethodId;
    private ChannelSoundingParams mChannelSoundingParams = null;

    /** @hide */
    public DistanceMeasurementParams(
            BluetoothDevice device, int duration, int frequency, int methodId) {
            BluetoothDevice device,
            int duration,
            int frequency,
            int methodId,
            ChannelSoundingParams channelSoundingParams) {
        mDevice = Objects.requireNonNull(device);
        mDuration = duration;
        mFrequency = frequency;
        mMethodId = methodId;
        mChannelSoundingParams = channelSoundingParams;
    }

    /**
@@ -128,6 +136,17 @@ public final class DistanceMeasurementParams implements Parcelable {
        return mMethodId;
    }

    /**
     * Returns {@link ChannelSoundingParams} of this DistanceMeasurementParams.
     *
     * @hide
     */
    @FlaggedApi("com.android.bluetooth.flags.channel_sounding")
    @SystemApi
    public @Nullable ChannelSoundingParams getChannelSoundingParams() {
        return mChannelSoundingParams;
    }

    /**
     * Get the default duration in seconds of the parameter.
     *
@@ -169,6 +188,7 @@ public final class DistanceMeasurementParams implements Parcelable {
        out.writeInt(mDuration);
        out.writeInt(mFrequency);
        out.writeInt(mMethodId);
        out.writeParcelable(mChannelSoundingParams, 0);
    }

    /** A {@link Parcelable.Creator} to create {@link DistanceMeasurementParams} from parcel. */
@@ -180,6 +200,8 @@ public final class DistanceMeasurementParams implements Parcelable {
                    builder.setDurationSeconds(in.readInt());
                    builder.setFrequency(in.readInt());
                    builder.setMethodId(in.readInt());
                    builder.setChannelSoundingParams(
                            (ChannelSoundingParams) in.readParcelable(null));
                    return builder.build();
                }

@@ -200,6 +222,7 @@ public final class DistanceMeasurementParams implements Parcelable {
        private int mDuration = REPORT_DURATION_DEFAULT;
        private int mFrequency = REPORT_FREQUENCY_LOW;
        private int mMethodId = DistanceMeasurementMethod.DISTANCE_MEASUREMENT_METHOD_RSSI;
        private ChannelSoundingParams mChannelSoundingParams = null;

        /**
         * Constructor of the Builder.
@@ -274,6 +297,21 @@ public final class DistanceMeasurementParams implements Parcelable {
            return this;
        }

        /**
         * Set {@link ChannelSoundingParams} for the DistanceMeasurementParams.
         *
         * @param channelSoundingParams parameters for Channel Sounding
         * @return the same Builder instance
         * @hide
         */
        @FlaggedApi("com.android.bluetooth.flags.channel_sounding")
        @SystemApi
        public @NonNull Builder setChannelSoundingParams(
                @NonNull ChannelSoundingParams channelSoundingParams) {
            mChannelSoundingParams = channelSoundingParams;
            return this;
        }

        /**
         * Build the {@link DistanceMeasurementParams} object.
         *
@@ -281,7 +319,8 @@ public final class DistanceMeasurementParams implements Parcelable {
         */
        @SystemApi
        public @NonNull DistanceMeasurementParams build() {
            return new DistanceMeasurementParams(mDevice, mDuration, mFrequency, mMethodId);
            return new DistanceMeasurementParams(
                    mDevice, mDuration, mFrequency, mMethodId, mChannelSoundingParams);
        }
    }
}