1 /* 2 * Copyright (C) 2021 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.ccc; 18 19 import android.os.Build.VERSION_CODES; 20 import android.os.PersistableBundle; 21 import android.uwb.RangingSession; 22 23 import androidx.annotation.RequiresApi; 24 25 import com.google.uwb.support.base.RequiredParam; 26 27 /** 28 * Defines parameters for CCC start reports. The start operation can optionally include a request to 29 * reconfigure the RAN multiplier. On a reconfiguration, the CCC spec defines that the selected RAN 30 * multiplier shall be equal to or greater than the requested RAN multiplier, and therefore, on a 31 * reconfiguration, the selected RAN multiplier shall be populated in the CCC start report. 32 * 33 * <p>This is passed as a bundle to the client callback {@link RangingSession.Callback#onStarted}. 34 */ 35 @RequiresApi(VERSION_CODES.LOLLIPOP) 36 public class CccRangingStartedParams extends CccParams { 37 private final int mStartingStsIndex; 38 private final long mUwbTime0; 39 private final int mHopModeKey; 40 @SyncCodeIndex private final int mSyncCodeIndex; 41 private final int mRanMultiplier; 42 43 private static final int BUNDLE_VERSION_1 = 1; 44 private static final int BUNDLE_VERSION_CURRENT = BUNDLE_VERSION_1; 45 46 private static final String KEY_STARTING_STS_INDEX = "starting_sts_index"; 47 private static final String KEY_UWB_TIME_0 = "uwb_time_0"; 48 private static final String KEY_HOP_MODE_KEY = "hop_mode_key"; 49 private static final String KEY_SYNC_CODE_INDEX = "sync_code_index"; 50 private static final String KEY_RAN_MULTIPLIER = "ran_multiplier"; 51 CccRangingStartedParams(Builder builder)52 private CccRangingStartedParams(Builder builder) { 53 mStartingStsIndex = builder.mStartingStsIndex.get(); 54 mUwbTime0 = builder.mUwbTime0.get(); 55 mHopModeKey = builder.mHopModeKey.get(); 56 mSyncCodeIndex = builder.mSyncCodeIndex.get(); 57 mRanMultiplier = builder.mRanMultiplier.get(); 58 } 59 60 @Override getBundleVersion()61 protected int getBundleVersion() { 62 return BUNDLE_VERSION_CURRENT; 63 } 64 65 @Override toBundle()66 public PersistableBundle toBundle() { 67 PersistableBundle bundle = super.toBundle(); 68 bundle.putInt(KEY_STARTING_STS_INDEX, mStartingStsIndex); 69 bundle.putLong(KEY_UWB_TIME_0, mUwbTime0); 70 bundle.putInt(KEY_HOP_MODE_KEY, mHopModeKey); 71 bundle.putInt(KEY_SYNC_CODE_INDEX, mSyncCodeIndex); 72 bundle.putInt(KEY_RAN_MULTIPLIER, mRanMultiplier); 73 return bundle; 74 } 75 fromBundle(PersistableBundle bundle)76 public static CccRangingStartedParams fromBundle(PersistableBundle bundle) { 77 if (!isCorrectProtocol(bundle)) { 78 throw new IllegalArgumentException("Invalid protocol"); 79 } 80 81 switch (getBundleVersion(bundle)) { 82 case BUNDLE_VERSION_1: 83 return parseVersion1(bundle); 84 85 default: 86 throw new IllegalArgumentException("Invalid bundle version"); 87 } 88 } 89 parseVersion1(PersistableBundle bundle)90 private static CccRangingStartedParams parseVersion1(PersistableBundle bundle) { 91 return new Builder() 92 .setStartingStsIndex(bundle.getInt(KEY_STARTING_STS_INDEX)) 93 .setUwbTime0(bundle.getLong(KEY_UWB_TIME_0)) 94 .setHopModeKey(bundle.getInt(KEY_HOP_MODE_KEY)) 95 .setSyncCodeIndex(bundle.getInt(KEY_SYNC_CODE_INDEX)) 96 .setRanMultiplier(bundle.getInt(KEY_RAN_MULTIPLIER)) 97 .build(); 98 } 99 getStartingStsIndex()100 public int getStartingStsIndex() { 101 return mStartingStsIndex; 102 } 103 getUwbTime0()104 public long getUwbTime0() { 105 return mUwbTime0; 106 } 107 getHopModeKey()108 public int getHopModeKey() { 109 return mHopModeKey; 110 } 111 112 @SyncCodeIndex getSyncCodeIndex()113 public int getSyncCodeIndex() { 114 return mSyncCodeIndex; 115 } 116 getRanMultiplier()117 public int getRanMultiplier() { 118 return mRanMultiplier; 119 } 120 121 /** Builder */ 122 public static final class Builder { 123 private RequiredParam<Integer> mStartingStsIndex = new RequiredParam<>(); 124 private RequiredParam<Long> mUwbTime0 = new RequiredParam<>(); 125 private RequiredParam<Integer> mHopModeKey = new RequiredParam<>(); 126 @SyncCodeIndex private RequiredParam<Integer> mSyncCodeIndex = new RequiredParam<>(); 127 private RequiredParam<Integer> mRanMultiplier = new RequiredParam<>(); 128 setStartingStsIndex(int startingStsIndex)129 public Builder setStartingStsIndex(int startingStsIndex) { 130 mStartingStsIndex.set(startingStsIndex); 131 return this; 132 } 133 setUwbTime0(long uwbTime0)134 public Builder setUwbTime0(long uwbTime0) { 135 mUwbTime0.set(uwbTime0); 136 return this; 137 } 138 setHopModeKey(int hopModeKey)139 public Builder setHopModeKey(int hopModeKey) { 140 mHopModeKey.set(hopModeKey); 141 return this; 142 } 143 setSyncCodeIndex(@yncCodeIndex int syncCodeIndex)144 public Builder setSyncCodeIndex(@SyncCodeIndex int syncCodeIndex) { 145 mSyncCodeIndex.set(syncCodeIndex); 146 return this; 147 } 148 setRanMultiplier(int ranMultiplier)149 public Builder setRanMultiplier(int ranMultiplier) { 150 mRanMultiplier.set(ranMultiplier); 151 return this; 152 } 153 build()154 public CccRangingStartedParams build() { 155 return new CccRangingStartedParams(this); 156 } 157 } 158 } 159