• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.ranging.ble.cs;
18 
19 import android.annotation.FlaggedApi;
20 import android.annotation.IntDef;
21 import android.annotation.NonNull;
22 import android.bluetooth.BluetoothAdapter;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 import android.ranging.RangingCapabilities.TechnologyCapabilities;
26 import android.ranging.RangingManager;
27 
28 import com.android.ranging.flags.Flags;
29 
30 import java.lang.annotation.ElementType;
31 import java.lang.annotation.Retention;
32 import java.lang.annotation.RetentionPolicy;
33 import java.lang.annotation.Target;
34 import java.util.ArrayList;
35 import java.util.HashSet;
36 import java.util.List;
37 import java.util.Set;
38 
39 /**
40  * Represents the capabilities of the Bluetooth-based Channel Sounding (CS) ranging.
41  */
42 @FlaggedApi(Flags.FLAG_RANGING_CS_ENABLED)
43 public final class BleCsRangingCapabilities implements Parcelable, TechnologyCapabilities {
44     /**
45      * @hide
46      */
47     @Retention(RetentionPolicy.SOURCE)
48     @Target({ElementType.TYPE_USE})
49     @IntDef({
50             CS_SECURITY_LEVEL_ONE,
51             CS_SECURITY_LEVEL_FOUR,
52     })
53     public @interface SecurityLevel {
54     }
55 
56     /**
57      * Security Level 1:
58      * Either CS tone or CS RTT..
59      */
60     public static final int CS_SECURITY_LEVEL_ONE = 1;
61 
62     /**
63      * Security Level 4:
64      * 10 ns CS RTT accuracy and CS tones with the addition of CS RTT sounding sequence or random
65      * sequence payloads, and support of the Normalized Attack Detector Metric requirements.
66      */
67     public static final int CS_SECURITY_LEVEL_FOUR = 4;
68 
69     private final List<Integer> mSupportedSecurityLevels;
70     private final String mBluetoothAddress;
71 
72     /**
73      * Returns a list of the supported security levels.
74      *
75      * @return a {@link Set} of integers representing the security levels,
76      * where each level is one of {@link SecurityLevel}.
77      */
78     @NonNull
79     @SecurityLevel
getSupportedSecurityLevels()80     public Set<Integer> getSupportedSecurityLevels() {
81         return new HashSet<>(mSupportedSecurityLevels);
82     }
83 
BleCsRangingCapabilities(Builder builder)84     private BleCsRangingCapabilities(Builder builder) {
85         mSupportedSecurityLevels = builder.mSupportedSecurityLevels;
86         mBluetoothAddress = builder.mBluetoothAddress;
87     }
88 
BleCsRangingCapabilities(Parcel in)89     private BleCsRangingCapabilities(Parcel in) {
90         mSupportedSecurityLevels = new ArrayList<>();
91         in.readList(mSupportedSecurityLevels, Integer.class.getClassLoader(), Integer.class);
92         mBluetoothAddress = in.readString();
93     }
94 
95     @NonNull
96     public static final Creator<BleCsRangingCapabilities> CREATOR =
97             new Creator<BleCsRangingCapabilities>() {
98                 @Override
99                 public BleCsRangingCapabilities createFromParcel(Parcel in) {
100                     return new BleCsRangingCapabilities(in);
101                 }
102 
103                 @Override
104                 public BleCsRangingCapabilities[] newArray(int size) {
105                     return new BleCsRangingCapabilities[size];
106                 }
107             };
108 
109     /**
110      * Get the device's bluetooth address.
111      * Internal usage only. Clients of the ranging API should use
112      * {@link BluetoothAdapter#getAddress()} instead.
113      *
114      * @hide
115      */
116     @NonNull
getBluetoothAddress()117     public String getBluetoothAddress() {
118         return mBluetoothAddress;
119     }
120 
121     /**
122      * @hide
123      */
124     @Override
getTechnology()125     public int getTechnology() {
126         return RangingManager.BLE_CS;
127     }
128 
129     /**
130      * @hide
131      */
132     @Override
describeContents()133     public int describeContents() {
134         return 0;
135     }
136 
137     @Override
writeToParcel(@onNull Parcel dest, int flags)138     public void writeToParcel(@NonNull Parcel dest, int flags) {
139         dest.writeList(mSupportedSecurityLevels);
140         dest.writeString(mBluetoothAddress);
141     }
142 
143     /**
144      * Builder class for {@link BleCsRangingCapabilities}.
145      * This class provides a fluent API for constructing instances of
146      * {@link BleCsRangingCapabilities}.
147      *
148      * @hide
149      */
150     public static final class Builder {
151         private List<Integer> mSupportedSecurityLevels;
152         private String mBluetoothAddress;
153 
154         /**
155          * Set supported security levels to the capabilities.
156          *
157          * @param supportedSecurityLevels the supported security levels {@link SecurityLevel}.
158          * @return this {@link Builder} instance for chaining calls.
159          * TODO(b/361634062): Make this a set in the API to match CS API.
160          */
161         @NonNull
setSupportedSecurityLevels(List<Integer> supportedSecurityLevels)162         public Builder setSupportedSecurityLevels(List<Integer> supportedSecurityLevels) {
163             this.mSupportedSecurityLevels = supportedSecurityLevels;
164             return this;
165         }
166 
167         /**
168          * Set the device's bluetooth address.
169          *
170          * @param address of the device.
171          * @return this {@link Builder} instance for chaining calls.
172          */
173         @NonNull
setBluetoothAddress(String address)174         public Builder setBluetoothAddress(String address) {
175             this.mBluetoothAddress = address;
176             return this;
177         }
178 
179         /**
180          * Builds and returns a {@link BleCsRangingCapabilities} instance.
181          *
182          * @return a new {@link BleCsRangingCapabilities} object.
183          */
184         @NonNull
build()185         public BleCsRangingCapabilities build() {
186             return new BleCsRangingCapabilities(this);
187         }
188     }
189 
190     @Override
toString()191     public String toString() {
192         return "BleCsRangingCapabilities{ "
193                 + "mBluetoothAddress="
194                 + mBluetoothAddress
195                 + ", mSupportedSecurityLevels="
196                 + mSupportedSecurityLevels
197                 + " }";
198     }
199 }
200