1 /* 2 * Copyright 2022 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; 18 19 import static com.android.server.uwb.data.UwbUciConstants.RANGING_MEASUREMENT_TYPE_TWO_WAY; 20 import static com.android.server.uwb.util.UwbUtil.convertFloatToQFormat; 21 import static com.android.server.uwb.util.UwbUtil.degreeToRadian; 22 23 import android.util.Pair; 24 import android.uwb.AngleMeasurement; 25 import android.uwb.AngleOfArrivalMeasurement; 26 import android.uwb.DistanceMeasurement; 27 import android.uwb.RangingMeasurement; 28 import android.uwb.RangingReport; 29 import android.uwb.UwbAddress; 30 31 import com.android.server.uwb.data.UwbRangingData; 32 import com.android.server.uwb.data.UwbTwoWayMeasurement; 33 import com.android.server.uwb.params.TlvUtil; 34 35 import com.google.uwb.support.fira.FiraParams; 36 37 public class UwbTestUtils { 38 private static final long TEST_SEQ_COUNTER = 5; 39 private static final long TEST_SESSION_ID = 7; 40 private static final int TEST_RCR_INDICATION = 7; 41 private static final long TEST_CURR_RANGING_INTERVAL = 100; 42 private static final int TEST_RANGING_MEASURES_TYPE = RANGING_MEASUREMENT_TYPE_TWO_WAY; 43 private static final int TEST_MAC_ADDRESS_MODE = 1; 44 private static final byte[] TEST_MAC_ADDRESS = {0x1, 0x3}; 45 private static final int TEST_STATUS = FiraParams.STATUS_CODE_OK; 46 private static final int TEST_LOS = 0; 47 private static final int TEST_DISTANCE = 101; 48 private static final float TEST_AOA_AZIMUTH = 67; 49 private static final int TEST_AOA_AZIMUTH_FOM = 50; 50 private static final float TEST_AOA_ELEVATION = 37; 51 private static final int TEST_AOA_ELEVATION_FOM = 90; 52 private static final float TEST_AOA_DEST_AZIMUTH = 67; 53 private static final int TEST_AOA_DEST_AZIMUTH_FOM = 50; 54 private static final float TEST_AOA_DEST_ELEVATION = 37; 55 private static final int TEST_AOA_DEST_ELEVATION_FOM = 90; 56 private static final int TEST_SLOT_IDX = 10; 57 UwbTestUtils()58 private UwbTestUtils() {} 59 generateRangingData(int rangingStatus)60 public static UwbRangingData generateRangingData(int rangingStatus) { 61 final int noOfRangingMeasures = 1; 62 final UwbTwoWayMeasurement[] uwbTwoWayMeasurements = 63 new UwbTwoWayMeasurement[noOfRangingMeasures]; 64 uwbTwoWayMeasurements[0] = new UwbTwoWayMeasurement(TEST_MAC_ADDRESS, rangingStatus, 65 TEST_LOS, TEST_DISTANCE, convertFloatToQFormat(TEST_AOA_AZIMUTH, 9, 7), 66 TEST_AOA_AZIMUTH_FOM, convertFloatToQFormat(TEST_AOA_ELEVATION, 9, 7), 67 TEST_AOA_ELEVATION_FOM, convertFloatToQFormat(TEST_AOA_DEST_AZIMUTH, 9, 7), 68 TEST_AOA_DEST_AZIMUTH_FOM, convertFloatToQFormat(TEST_AOA_DEST_ELEVATION, 9, 7), 69 TEST_AOA_DEST_ELEVATION_FOM, TEST_SLOT_IDX); 70 return new UwbRangingData(TEST_SEQ_COUNTER, TEST_SESSION_ID, 71 TEST_RCR_INDICATION, TEST_CURR_RANGING_INTERVAL, TEST_RANGING_MEASURES_TYPE, 72 TEST_MAC_ADDRESS_MODE, noOfRangingMeasures, uwbTwoWayMeasurements); 73 } 74 75 // Helper method to generate a UwbRangingData instance and corresponding RangingMeasurement generateRangingDataAndRangingReport( boolean isAoaAzimuthEnabled, boolean isAoaElevationEnabled, boolean isDestAoaAzimuthEnabled, boolean isDestAoaElevationEnabled, long elapsedRealtimeNanos)76 public static Pair<UwbRangingData, RangingReport> generateRangingDataAndRangingReport( 77 boolean isAoaAzimuthEnabled, boolean isAoaElevationEnabled, 78 boolean isDestAoaAzimuthEnabled, boolean isDestAoaElevationEnabled, 79 long elapsedRealtimeNanos) { 80 UwbRangingData uwbRangingData = generateRangingData(TEST_STATUS); 81 82 AngleOfArrivalMeasurement aoaMeasurement = null; 83 AngleOfArrivalMeasurement aoaDestMeasurement = null; 84 if (isAoaAzimuthEnabled || isAoaElevationEnabled) { 85 AngleMeasurement aoaAzimuth = null; 86 AngleMeasurement aoaElevation = null; 87 if (isAoaAzimuthEnabled) { 88 aoaAzimuth = 89 new AngleMeasurement( 90 degreeToRadian(TEST_AOA_AZIMUTH), 0, 91 TEST_AOA_AZIMUTH_FOM / (double) 100); 92 } 93 if (isAoaElevationEnabled) { 94 aoaElevation = 95 new AngleMeasurement( 96 degreeToRadian(TEST_AOA_ELEVATION), 0, 97 TEST_AOA_ELEVATION_FOM / (double) 100); 98 } 99 aoaMeasurement = new AngleOfArrivalMeasurement.Builder(aoaAzimuth) 100 .setAltitude(aoaElevation) 101 .build(); 102 } 103 if (isDestAoaAzimuthEnabled || isDestAoaElevationEnabled) { 104 AngleMeasurement aoaDestAzimuth = null; 105 AngleMeasurement aoaDestElevation = null; 106 if (isDestAoaAzimuthEnabled) { 107 aoaDestAzimuth = 108 new AngleMeasurement( 109 degreeToRadian(TEST_AOA_DEST_AZIMUTH), 0, 110 TEST_AOA_DEST_AZIMUTH_FOM / (double) 100); 111 } 112 if (isDestAoaElevationEnabled) { 113 aoaDestElevation = 114 new AngleMeasurement( 115 degreeToRadian(TEST_AOA_DEST_ELEVATION), 0, 116 TEST_AOA_DEST_ELEVATION_FOM / (double) 100); 117 } 118 aoaDestMeasurement = new AngleOfArrivalMeasurement.Builder(aoaDestAzimuth) 119 .setAltitude(aoaDestElevation) 120 .build(); 121 } 122 RangingMeasurement rangingMeasurement = new RangingMeasurement.Builder() 123 .setRemoteDeviceAddress(UwbAddress.fromBytes( 124 TlvUtil.getReverseBytes(TEST_MAC_ADDRESS))) 125 .setStatus(TEST_STATUS) 126 .setElapsedRealtimeNanos(elapsedRealtimeNanos) 127 .setDistanceMeasurement( 128 new DistanceMeasurement.Builder() 129 .setMeters(TEST_DISTANCE / (double) 100) 130 .setErrorMeters(0) 131 .setConfidenceLevel(0) 132 .build()) 133 .setAngleOfArrivalMeasurement(aoaMeasurement) 134 .setDestinationAngleOfArrivalMeasurement(aoaDestMeasurement) 135 .setLineOfSight(TEST_LOS) 136 .build(); 137 RangingReport rangingReport = new RangingReport.Builder() 138 .addMeasurement(rangingMeasurement) 139 .build(); 140 return Pair.create(uwbRangingData, rangingReport); 141 } 142 } 143