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.android.server.uwb.params; 18 19 import android.uwb.UwbAddress; 20 21 import com.android.server.uwb.config.ConfigParam; 22 23 import com.google.uwb.support.base.Params; 24 import com.google.uwb.support.fira.FiraOpenSessionParams; 25 import com.google.uwb.support.fira.FiraParams; 26 import com.google.uwb.support.fira.FiraRangingReconfigureParams; 27 28 import java.nio.ByteBuffer; 29 import java.util.Arrays; 30 31 public class FiraEncoder extends TlvEncoder { 32 @Override getTlvBuffer(Params param)33 public TlvBuffer getTlvBuffer(Params param) { 34 if (param instanceof FiraOpenSessionParams) { 35 return getTlvBufferFromFiraOpenSessionParams(param); 36 } 37 38 if (param instanceof FiraRangingReconfigureParams) { 39 return getTlvBufferFromFiraRangingReconfigureParams(param); 40 } 41 return null; 42 } 43 getTlvBufferFromFiraOpenSessionParams(Params baseParam)44 private TlvBuffer getTlvBufferFromFiraOpenSessionParams(Params baseParam) { 45 FiraOpenSessionParams params = (FiraOpenSessionParams) baseParam; 46 ByteBuffer dstAddressList = ByteBuffer.allocate(1024); 47 for (UwbAddress address : params.getDestAddressList()) { 48 dstAddressList.put(TlvUtil.getReverseBytes(address.toBytes())); 49 } 50 51 int resultReportConfig = getResultReportConfig(params); 52 int rangingRoundControl = getRangingRoundControl(params); 53 54 TlvBuffer tlvBuffer = new TlvBuffer.Builder() 55 .putByte(ConfigParam.DEVICE_TYPE, (byte) params.getDeviceType()) 56 .putByte(ConfigParam.RANGING_ROUND_USAGE, (byte) params.getRangingRoundUsage()) 57 .putByte(ConfigParam.STS_CONFIG, (byte) params.getStsConfig()) 58 .putByte(ConfigParam.MULTI_NODE_MODE, (byte) params.getMultiNodeMode()) 59 .putByte(ConfigParam.CHANNEL_NUMBER, (byte) params.getChannelNumber()) 60 .putByte(ConfigParam.NUMBER_OF_CONTROLEES, 61 (byte) params.getDestAddressList().size()) 62 .putByteArray(ConfigParam.DEVICE_MAC_ADDRESS, params.getDeviceAddress().size(), 63 TlvUtil.getReverseBytes(params.getDeviceAddress().toBytes())) 64 .putByteArray(ConfigParam.DST_MAC_ADDRESS, dstAddressList.position(), 65 Arrays.copyOf(dstAddressList.array(), dstAddressList.position())) 66 .putShort(ConfigParam.SLOT_DURATION, (short) params.getSlotDurationRstu()) 67 .putInt(ConfigParam.RANGING_INTERVAL, params.getRangingIntervalMs()) 68 .putByte(ConfigParam.MAC_FCS_TYPE, (byte) params.getFcsType()) 69 .putByte(ConfigParam.RANGING_ROUND_CONTROL, 70 (byte) rangingRoundControl/* params.getMeasurementReportType()*/) 71 .putByte(ConfigParam.AOA_RESULT_REQ, (byte) params.getAoaResultRequest()) 72 .putByte(ConfigParam.RANGE_DATA_NTF_CONFIG, (byte) params.getRangeDataNtfConfig()) 73 .putShort(ConfigParam.RANGE_DATA_NTF_PROXIMITY_NEAR, 74 (short) params.getRangeDataNtfProximityNear()) 75 .putShort(ConfigParam.RANGE_DATA_NTF_PROXIMITY_FAR, 76 (short) params.getRangeDataNtfProximityFar()) 77 .putByte(ConfigParam.DEVICE_ROLE, (byte) params.getDeviceRole()) 78 .putByte(ConfigParam.RFRAME_CONFIG, (byte) params.getRframeConfig()) 79 .putByte(ConfigParam.PREAMBLE_CODE_INDEX, (byte) params.getPreambleCodeIndex()) 80 .putByte(ConfigParam.SFD_ID, (byte) params.getSfdId()) 81 .putByte(ConfigParam.PSDU_DATA_RATE, (byte) params.getPsduDataRate()) 82 .putByte(ConfigParam.PREAMBLE_DURATION, (byte) params.getPreambleDuration()) 83 .putByte(ConfigParam.SLOTS_PER_RR, (byte) params.getSlotsPerRangingRound()) 84 .putByte(ConfigParam.TX_ADAPTIVE_PAYLOAD_POWER, 85 params.isTxAdaptivePayloadPowerEnabled() ? (byte) 1 : (byte) 0) 86 .putByte(ConfigParam.PRF_MODE, (byte) params.getPrfMode()) 87 .putByte(ConfigParam.KEY_ROTATION, 88 params.isKeyRotationEnabled() ? (byte) 1 : (byte) 0) 89 .putByte(ConfigParam.KEY_ROTATION_RATE, (byte) params.getKeyRotationRate()) 90 .putByte(ConfigParam.SESSION_PRIORITY, (byte) params.getSessionPriority()) 91 .putByte(ConfigParam.MAC_ADDRESS_MODE, (byte) params.getMacAddressMode()) 92 .putByteArray(ConfigParam.VENDOR_ID, 93 TlvUtil.getReverseBytes(params.getVendorId())) 94 .putByteArray(ConfigParam.STATIC_STS_IV, 95 params.getStaticStsIV()) 96 .putByte(ConfigParam.NUMBER_OF_STS_SEGMENTS, (byte) params.getStsSegmentCount()) 97 .putShort(ConfigParam.MAX_RR_RETRY, (short) params.getMaxRangingRoundRetries()) 98 .putInt(ConfigParam.UWB_INITIATION_TIME, params.getInitiationTimeMs()) 99 .putByte(ConfigParam.HOPPING_MODE, 100 (byte) params.getHoppingMode()) 101 .putByte(ConfigParam.BLOCK_STRIDE_LENGTH, (byte) params.getBlockStrideLength()) 102 .putByte(ConfigParam.RESULT_REPORT_CONFIG, (byte) resultReportConfig) 103 .putByte(ConfigParam.IN_BAND_TERMINATION_ATTEMPT_COUNT, 104 (byte) params.getInBandTerminationAttemptCount()) 105 .putInt(ConfigParam.SUB_SESSION_ID, params.getSubSessionId()) 106 .putByte(ConfigParam.BPRF_PHR_DATA_RATE, (byte) params.getBprfPhrDataRate()) 107 .putByte(ConfigParam.STS_LENGTH, (byte) params.getStsLength()) 108 .putByte(ConfigParam.NUM_RANGE_MEASUREMENTS, 109 (byte) params.getNumOfMsrmtFocusOnRange()) 110 .putByte(ConfigParam.NUM_AOA_AZIMUTH_MEASUREMENTS, 111 (byte) params.getNumOfMsrmtFocusOnAoaAzimuth()) 112 .putByte(ConfigParam.NUM_AOA_ELEVATION_MEASUREMENTS, 113 (byte) params.getNumOfMsrmtFocusOnAoaElevation()) 114 .build(); 115 return tlvBuffer; 116 } 117 getTlvBufferFromFiraRangingReconfigureParams(Params baseParam)118 private TlvBuffer getTlvBufferFromFiraRangingReconfigureParams(Params baseParam) { 119 FiraRangingReconfigureParams params = (FiraRangingReconfigureParams) baseParam; 120 TlvBuffer.Builder tlvBuilder = new TlvBuffer.Builder(); 121 Integer blockStrideLength = params.getBlockStrideLength(); 122 Integer rangeDataNtfConfig = params.getRangeDataNtfConfig(); 123 Integer rangeDataProximityNear = params.getRangeDataProximityNear(); 124 Integer rangeDataProximityFar = params.getRangeDataProximityFar(); 125 126 if (blockStrideLength != null) { 127 tlvBuilder.putByte(ConfigParam.BLOCK_STRIDE_LENGTH, 128 (byte) blockStrideLength.intValue()); 129 } 130 131 if (rangeDataNtfConfig != null) { 132 tlvBuilder.putByte(ConfigParam.RANGE_DATA_NTF_CONFIG, 133 (byte) rangeDataNtfConfig.intValue()); 134 } 135 136 if (rangeDataProximityNear != null) { 137 tlvBuilder.putShort(ConfigParam.RANGE_DATA_NTF_PROXIMITY_NEAR, 138 (short) rangeDataProximityNear.intValue()); 139 } 140 141 if (rangeDataProximityFar != null) { 142 tlvBuilder.putShort(ConfigParam.RANGE_DATA_NTF_PROXIMITY_FAR, 143 (short) rangeDataProximityFar.intValue()); 144 } 145 146 return tlvBuilder.build(); 147 } 148 149 // Merged data from other parameter values getResultReportConfig(FiraOpenSessionParams params)150 private int getResultReportConfig(FiraOpenSessionParams params) { 151 int resultReportConfig = 0x00; 152 resultReportConfig |= params.hasTimeOfFlightReport() ? 0x01 : 0x00; 153 resultReportConfig |= params.hasAngleOfArrivalAzimuthReport() ? 0x02 : 0x00; 154 resultReportConfig |= params.hasAngleOfArrivalElevationReport() ? 0x04 : 0x00; 155 resultReportConfig |= params.hasAngleOfArrivalFigureOfMeritReport() ? 0x08 : 0x00; 156 return resultReportConfig; 157 } 158 getRangingRoundControl(FiraOpenSessionParams params)159 private int getRangingRoundControl(FiraOpenSessionParams params) { 160 //RANGING_ROUND_CONTROL 161 int rangingRoundControl = 0x02; 162 163 // b0 : Ranging Result Report Message 164 rangingRoundControl |= params.hasResultReportPhase() ? 0x01 : 0x00; 165 166 // b7 : Measurement Report Message 167 if (params.getMeasurementReportType() 168 == FiraParams.MEASUREMENT_REPORT_TYPE_RESPONDER_TO_INITIATOR) { 169 rangingRoundControl |= 0x80; 170 } 171 return rangingRoundControl; 172 } 173 } 174