1 /* 2 * Copyright 2025 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 com.android.server.ranging.cs; 18 19 import com.android.server.ranging.RangingTechnology; 20 import com.android.server.ranging.oob.SetConfigurationMessage; 21 import com.android.server.ranging.oob.TechnologyHeader; 22 23 import com.google.auto.value.AutoValue; 24 import com.google.common.collect.ImmutableList; 25 26 import java.nio.ByteBuffer; 27 28 /** Configuration for UWB sent as part SetConfigurationMessage for Finder OOB. */ 29 @AutoValue 30 public abstract class CsOobConfig implements SetConfigurationMessage.TechnologyOobConfig { 31 32 private static final int EXPECTED_SIZE_BYTES = 2; 33 34 /** Returns the size of the object in bytes when serialized. */ getSize()35 public final int getSize() { 36 return EXPECTED_SIZE_BYTES; 37 } 38 39 /** Enum representing security type for Channel Sounding. */ 40 public enum CsSecurityType { 41 UNKNOWN(0), 42 LEVEL_ONE(1), 43 LEVEL_TWO(2), 44 LEVEL_THREE(3), 45 LEVEL_FOUR(4); 46 47 public static final ImmutableList<CsSecurityType> SECURITY_TYPES = 48 ImmutableList.copyOf(CsSecurityType.values()); 49 50 private final int mValue; 51 CsSecurityType(int value)52 CsSecurityType(int value) { 53 this.mValue = value; 54 } 55 getValue()56 public int getValue() { 57 return mValue; 58 } 59 fromValue(int value)60 public static CsSecurityType fromValue(int value) { 61 return value < 0 || value > LEVEL_FOUR.mValue ? UNKNOWN 62 : CsSecurityType.values()[value]; 63 } 64 } 65 66 /** Enum representing BR/EDR support and capability flag for Channel Sounding. */ 67 public enum CsLeFlag { 68 LE_FLAG_LIMITED_DISCOVERY_MODE(0), 69 LE_FLAG_GENERAL_DISCOVERY_MODE(1), 70 LE_FLAG_BREDR_NOT_SUPPORTED(2), 71 LE_FLAG_SIMULTANEOUS_CONTROLLER(3), 72 LE_FLAG_SIMULTANEOUS_HOST(4), 73 UNKNOWN(5); 74 75 private final int mValue; 76 CsLeFlag(int value)77 CsLeFlag(int value) { 78 this.mValue = value; 79 } 80 toByte()81 public byte toByte() { 82 return (byte) mValue; 83 } 84 parseByte(byte leFlagByte)85 public static CsLeFlag parseByte(byte leFlagByte) { 86 if (leFlagByte < 0 || leFlagByte > UNKNOWN.mValue) { 87 return UNKNOWN; 88 } 89 return CsLeFlag.values()[leFlagByte]; 90 } 91 } 92 93 /** 94 * Parses the given byte array and returns {@link CsOobConfig} object. Throws {@link 95 * IllegalArgumentException} on invalid input. 96 */ parseBytes(byte[] csConfigBytes)97 public static CsOobConfig parseBytes(byte[] csConfigBytes) { 98 TechnologyHeader header = TechnologyHeader.parseBytes(csConfigBytes); 99 100 if (csConfigBytes.length < EXPECTED_SIZE_BYTES) { 101 throw new IllegalArgumentException( 102 String.format( 103 "CsOobConfig size is %d, expected at least %d", 104 csConfigBytes.length, EXPECTED_SIZE_BYTES)); 105 } 106 107 if (header.getRangingTechnology() != RangingTechnology.CS) { 108 throw new IllegalArgumentException( 109 String.format( 110 "CsOobConfig header technology field is %s, expected %s", 111 header.getRangingTechnology(), RangingTechnology.CS)); 112 } 113 114 int parseCursor = header.getHeaderSize(); 115 116 return builder().build(); 117 } 118 119 /** Serializes this {@link CsOobConfig} object to bytes. */ toBytes()120 public final byte[] toBytes() { 121 return ByteBuffer.allocate(EXPECTED_SIZE_BYTES) 122 .put(RangingTechnology.CS.toByte()) 123 .put((byte) EXPECTED_SIZE_BYTES) 124 .array(); 125 } 126 127 /** Returns a builder for {@link CsOobConfig}. */ builder()128 public static Builder builder() { 129 return new AutoValue_CsOobConfig.Builder(); 130 } 131 132 /** Builder for {@link CsOobConfig}. */ 133 @AutoValue.Builder 134 public abstract static class Builder { build()135 public abstract CsOobConfig build(); 136 } 137 } 138