1 /* 2 * Copyright (C) 2023 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.google.uwb.support.rftest; 18 19 import android.os.PersistableBundle; 20 21 import androidx.annotation.NonNull; 22 23 import com.google.uwb.support.base.RequiredParam; 24 25 /** 26 * Defines parameters to open a Rftest session. 27 * 28 * <p>This is passed as a bundle to the service API {@link UwbSessionManager#start}. 29 */ 30 public class RfTestStartSessionParams extends RfTestParams { 31 private static final int BUNDLE_VERSION_1 = 1; 32 private static final int BUNDLE_VERSION_CURRENT = BUNDLE_VERSION_1; 33 34 private static final String KEY_RF_OPERATION_TYPE = "rf_operation_type"; 35 private static final String KEY_PSDU_DATA = "psdu_data"; 36 37 @RfTestOperationType private final int mRfTestOperationType; 38 private final byte[] mPsduData; 39 RfTestStartSessionParams( int rfTestOperationType, byte[] psduData)40 private RfTestStartSessionParams( 41 int rfTestOperationType, 42 byte[] psduData) { 43 mRfTestOperationType = rfTestOperationType; 44 mPsduData = psduData; 45 } 46 47 @Override getBundleVersion()48 protected int getBundleVersion() { 49 return BUNDLE_VERSION_CURRENT; 50 } 51 52 @Override toBundle()53 public PersistableBundle toBundle() { 54 PersistableBundle bundle = super.toBundle(); 55 bundle.putInt(KEY_RF_OPERATION_TYPE, mRfTestOperationType); 56 bundle.putIntArray(KEY_PSDU_DATA, RfTestParams.byteArrayToIntArray(mPsduData)); 57 return bundle; 58 } 59 60 /** Unpack the {@link PersistableBundle} to a {@link RfTestStartSessionParams} */ fromBundle(PersistableBundle bundle)61 public static RfTestStartSessionParams fromBundle(PersistableBundle bundle) { 62 if (!isCorrectProtocol(bundle)) { 63 throw new IllegalArgumentException("Invalid protocol"); 64 } 65 66 switch (getBundleVersion(bundle)) { 67 case BUNDLE_VERSION_1: 68 return parseBundleVersion1(bundle); 69 70 default: 71 throw new IllegalArgumentException("unknown bundle version"); 72 } 73 } 74 parseBundleVersion1(PersistableBundle bundle)75 private static RfTestStartSessionParams parseBundleVersion1(PersistableBundle bundle) { 76 77 return new Builder() 78 .setRfTestOperationType(bundle.getInt(KEY_RF_OPERATION_TYPE)) 79 .setPsduData(RfTestParams.intArrayToByteArray(bundle.getIntArray(KEY_PSDU_DATA))) 80 .build(); 81 } 82 83 @RfTestOperationType getRfTestOperationType()84 public int getRfTestOperationType() { 85 return mRfTestOperationType; 86 } 87 getPsduData()88 public byte[] getPsduData() { 89 return mPsduData; 90 } 91 92 /** Builder */ 93 public static final class Builder { 94 @RfTestOperationType 95 private RequiredParam<Integer> mRfTestOperationType = 96 new RequiredParam<Integer>(); 97 98 private byte[] mPsduData = new byte[0]; 99 Builder()100 public Builder() {} 101 Builder(@onNull Builder builder)102 public Builder(@NonNull Builder builder) { 103 mRfTestOperationType.set(builder.mRfTestOperationType.get()); 104 mPsduData = builder.mPsduData; 105 } 106 Builder(@onNull RfTestStartSessionParams params)107 public Builder(@NonNull RfTestStartSessionParams params) { 108 mRfTestOperationType.set(params.mRfTestOperationType); 109 mPsduData = params.mPsduData; 110 } 111 setRfTestOperationType(@fTestOperationType int rfTestOperationType)112 public Builder setRfTestOperationType(@RfTestOperationType int rfTestOperationType) { 113 mRfTestOperationType.set(rfTestOperationType); 114 return this; 115 } 116 setPsduData(byte[] psduData)117 public Builder setPsduData(byte[] psduData) { 118 mPsduData = psduData; 119 return this; 120 } 121 122 /** Build {@link RfTestStartSessionParams} */ build()123 public RfTestStartSessionParams build() { 124 return new RfTestStartSessionParams( 125 mRfTestOperationType.get(), 126 mPsduData); 127 } 128 } 129 } 130