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.oob; 18 19 import android.annotation.FlaggedApi; 20 import android.annotation.IntDef; 21 import android.annotation.NonNull; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 import android.ranging.RangingConfig; 25 import android.util.Range; 26 27 import com.android.ranging.flags.Flags; 28 29 import java.lang.annotation.Retention; 30 import java.lang.annotation.RetentionPolicy; 31 import java.time.Duration; 32 import java.util.ArrayList; 33 import java.util.List; 34 35 /** 36 * Represents the configuration for an Out-of-Band (OOB) initiator in a ranging session. 37 * This class includes configuration options such as device handles, security level, 38 * ranging mode, and interval range for setting up an OOB initiator ranging session. 39 */ 40 @FlaggedApi(Flags.FLAG_RANGING_STACK_ENABLED) 41 public final class OobInitiatorRangingConfig extends RangingConfig implements Parcelable { 42 43 /** 44 * @hide 45 */ 46 @Retention(RetentionPolicy.SOURCE) 47 @IntDef({ 48 SECURITY_LEVEL_BASIC, 49 SECURITY_LEVEL_SECURE, 50 }) 51 public @interface SecurityLevel { 52 } 53 54 /** 55 * Basic security level for the ranging session. 56 * <p>Example usage: 57 * UWB: Static-STS 58 * BLE-CS: Security level one 59 */ 60 public static final int SECURITY_LEVEL_BASIC = 0; 61 62 /** 63 * Basic security level for the ranging session. 64 * <p>Example usage: 65 * UWB: Provisioned-STS 66 * BLE-CS: Security level four 67 */ 68 public static final int SECURITY_LEVEL_SECURE = 1; 69 70 /** 71 * @hide 72 */ 73 @Retention(RetentionPolicy.SOURCE) 74 @IntDef({ 75 RANGING_MODE_AUTO, 76 RANGING_MODE_HIGH_ACCURACY, 77 RANGING_MODE_HIGH_ACCURACY_PREFERRED, 78 RANGING_MODE_FUSED, 79 }) 80 public @interface RangingMode { 81 } 82 83 /** 84 * Automatic ranging mode. Allows the system to choose the best mode. 85 */ 86 public static final int RANGING_MODE_AUTO = 0; 87 /** 88 * High accuracy ranging mode. No fallback allowed. 89 */ 90 public static final int RANGING_MODE_HIGH_ACCURACY = 1; 91 /** 92 * High accuracy ranging mode. Fallback to lower accuracy if high accuracy ranging is not 93 * supported by all devices. 94 */ 95 public static final int RANGING_MODE_HIGH_ACCURACY_PREFERRED = 2; 96 /** 97 * Starts ranging with all the ranging technologies both devices support. 98 */ 99 public static final int RANGING_MODE_FUSED = 3; 100 101 private final List<DeviceHandle> mDeviceHandles; 102 103 private final Range<Duration> mRangingIntervalRange; 104 105 @SecurityLevel 106 private final int mSecurityLevel; 107 108 @RangingMode 109 private final int mRangingMode; 110 OobInitiatorRangingConfig(Builder builder)111 private OobInitiatorRangingConfig(Builder builder) { 112 setRangingSessionType(RangingConfig.RANGING_SESSION_OOB); 113 mDeviceHandles = new ArrayList<>(builder.mDeviceHandles); 114 mSecurityLevel = builder.mSecurityLevel; 115 mRangingMode = builder.mRangingMode; 116 mRangingIntervalRange = new Range<>(builder.mFastestRangingInterval, 117 builder.mSlowestRangingInterval); 118 } 119 OobInitiatorRangingConfig(Parcel in)120 private OobInitiatorRangingConfig(Parcel in) { 121 setRangingSessionType(in.readInt()); 122 mDeviceHandles = in.createTypedArrayList(DeviceHandle.CREATOR); 123 mSecurityLevel = in.readInt(); 124 mRangingMode = in.readInt(); 125 Duration lower = Duration.ofMillis(in.readLong()); 126 Duration upper = Duration.ofMillis(in.readLong()); 127 mRangingIntervalRange = new Range<>(lower, upper); 128 129 } 130 131 @Override writeToParcel(@onNull Parcel dest, int flags)132 public void writeToParcel(@NonNull Parcel dest, int flags) { 133 dest.writeInt(getRangingSessionType()); 134 dest.writeTypedList(mDeviceHandles); 135 dest.writeInt(mSecurityLevel); 136 dest.writeInt(mRangingMode); 137 dest.writeLong(mRangingIntervalRange.getLower().toMillis()); 138 dest.writeLong(mRangingIntervalRange.getUpper().toMillis()); 139 } 140 141 @Override describeContents()142 public int describeContents() { 143 return 0; 144 } 145 146 @NonNull 147 public static final Creator<OobInitiatorRangingConfig> CREATOR = 148 new Creator<OobInitiatorRangingConfig>() { 149 @Override 150 public OobInitiatorRangingConfig createFromParcel(Parcel in) { 151 return new OobInitiatorRangingConfig(in); 152 } 153 154 @Override 155 public OobInitiatorRangingConfig[] newArray(int size) { 156 return new OobInitiatorRangingConfig[size]; 157 } 158 }; 159 160 /** 161 * Returns the list of DeviceHandles associated with the OOB initiator. 162 * 163 * @return A list of DeviceHandle objects. 164 */ 165 @NonNull getDeviceHandles()166 public List<DeviceHandle> getDeviceHandles() { 167 return mDeviceHandles; 168 } 169 170 /** 171 * Returns the ranging interval range configuration. 172 * 173 * @return The {@link Range} associated with this OOB initiator. 174 */ 175 @NonNull getRangingIntervalRange()176 public Range<Duration> getRangingIntervalRange() { 177 return mRangingIntervalRange; 178 } 179 180 /** 181 * Returns the fastest requested ranging interval. 182 * 183 * @return The fastest interval. 184 */ 185 @NonNull getFastestRangingInterval()186 public Duration getFastestRangingInterval() { 187 return mRangingIntervalRange.getLower(); 188 } 189 190 /** 191 * Returns the slowest acceptable ranging. 192 * 193 * @return The slowest interval. 194 */ 195 @NonNull getSlowestRangingInterval()196 public Duration getSlowestRangingInterval() { 197 return mRangingIntervalRange.getUpper(); 198 } 199 200 /** 201 * Returns the security level set for the ranging session. 202 * 203 * @return the security level. 204 * <p>Possible values: 205 * {@link #SECURITY_LEVEL_BASIC} 206 * {@link #SECURITY_LEVEL_SECURE} 207 */ 208 @SecurityLevel getSecurityLevel()209 public int getSecurityLevel() { 210 return mSecurityLevel; 211 } 212 213 /** 214 * Returns the ranging mode for the session. 215 * 216 * @return the ranging mode. 217 * <p>Possible values: 218 * {@link #RANGING_MODE_AUTO} 219 * {@link #RANGING_MODE_HIGH_ACCURACY} 220 * {@link #RANGING_MODE_HIGH_ACCURACY_PREFERRED} 221 * {@link #RANGING_MODE_FUSED} 222 */ 223 224 @RangingMode getRangingMode()225 public int getRangingMode() { 226 return mRangingMode; 227 } 228 229 /** 230 * Builder class for creating instances of {@link OobInitiatorRangingConfig}. 231 */ 232 public static final class Builder { 233 private final List<DeviceHandle> mDeviceHandles = new ArrayList<>(); 234 @SecurityLevel 235 private int mSecurityLevel = SECURITY_LEVEL_BASIC; 236 @RangingMode 237 private int mRangingMode = RANGING_MODE_AUTO; 238 239 private Duration mFastestRangingInterval = Duration.ofMillis(100); 240 private Duration mSlowestRangingInterval = Duration.ofMillis(5000); 241 242 /** 243 * Sets the fastest ranging interval in milliseconds. 244 * 245 * @param intervalMs The fastest interval in milliseconds. 246 * Defaults to 100ms 247 * @return The Builder instance, for chaining calls. 248 */ 249 @NonNull setFastestRangingInterval(@onNull Duration intervalMs)250 public Builder setFastestRangingInterval(@NonNull Duration intervalMs) { 251 this.mFastestRangingInterval = intervalMs; 252 return this; 253 } 254 255 /** 256 * Sets the slowest ranging interval in milliseconds. 257 * 258 * @param intervalMs The slowest interval in milliseconds. 259 * Defaults to 5000ms 260 * @return The Builder instance, for chaining calls. 261 */ 262 @NonNull setSlowestRangingInterval(@onNull Duration intervalMs)263 public Builder setSlowestRangingInterval(@NonNull Duration intervalMs) { 264 if (intervalMs.isNegative() || intervalMs.isZero()) { 265 throw new IllegalArgumentException("Slowest duration cannot be negative or zero"); 266 } 267 this.mSlowestRangingInterval = intervalMs; 268 return this; 269 } 270 271 /** 272 * Adds a DeviceHandle to the list of devices for the ranging session. 273 * 274 * @param deviceHandle The DeviceHandle to add. 275 * @return The Builder instance. 276 */ 277 @NonNull addDeviceHandle(@onNull DeviceHandle deviceHandle)278 public Builder addDeviceHandle(@NonNull DeviceHandle deviceHandle) { 279 mDeviceHandles.add(deviceHandle); 280 return this; 281 } 282 283 /** 284 * Adds a list of DeviceHandle to the list of devices for the ranging session. 285 * 286 * @param deviceHandles The list of DeviceHandles to add. 287 * @return The Builder instance. 288 */ 289 @NonNull addDeviceHandles(@onNull List<DeviceHandle> deviceHandles)290 public Builder addDeviceHandles(@NonNull List<DeviceHandle> deviceHandles) { 291 mDeviceHandles.addAll(deviceHandles); 292 return this; 293 } 294 295 /** 296 * Sets the security level for the ranging session. 297 * 298 * @param securityLevel The security level to set. 299 * Defaults to {@link #SECURITY_LEVEL_BASIC} 300 * @return The Builder instance. 301 */ 302 @NonNull setSecurityLevel(@ecurityLevel int securityLevel)303 public Builder setSecurityLevel(@SecurityLevel int securityLevel) { 304 this.mSecurityLevel = securityLevel; 305 return this; 306 } 307 308 /** 309 * Sets the ranging mode for the session. 310 * 311 * @param rangingMode The ranging mode to set. 312 * Defaults to {@link #RANGING_MODE_AUTO} 313 * @return The Builder instance. 314 */ 315 @NonNull setRangingMode(@angingMode int rangingMode)316 public Builder setRangingMode(@RangingMode int rangingMode) { 317 this.mRangingMode = rangingMode; 318 return this; 319 } 320 321 /** 322 * Builds an instance of {@link OobInitiatorRangingConfig} with the provided parameters. 323 * 324 * @return A new OobInitiatorRangingConfig instance. 325 */ 326 @NonNull build()327 public OobInitiatorRangingConfig build() { 328 if (mDeviceHandles.isEmpty()) { 329 throw new IllegalArgumentException("Device handle list cannot be empty"); 330 } 331 return new OobInitiatorRangingConfig(this); 332 } 333 } 334 335 @Override toString()336 public String toString() { 337 return "OobInitiatorRangingConfig{ " 338 + "mDeviceHandles=" 339 + mDeviceHandles 340 + ", mRangingIntervalRange=" 341 + mRangingIntervalRange 342 + ", mSecurityLevel=" 343 + mSecurityLevel 344 + ", mRangingMode=" 345 + mRangingMode 346 + ", " 347 + super.toString() 348 + ", " 349 + " }"; 350 } 351 } 352