• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 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 android.uwb;
18 
19 import android.os.SystemClock;
20 
21 import java.util.concurrent.Executor;
22 
23 public class UwbTestUtils {
UwbTestUtils()24     private UwbTestUtils() {}
25 
getAngleMeasurement()26     public static AngleMeasurement getAngleMeasurement() {
27         return new AngleMeasurement(
28                 getDoubleInRange(-Math.PI, Math.PI),
29                 getDoubleInRange(0, Math.PI),
30                 getDoubleInRange(0, 1));
31     }
32 
getAngleOfArrivalMeasurement()33     public static AngleOfArrivalMeasurement getAngleOfArrivalMeasurement() {
34         return new AngleOfArrivalMeasurement.Builder(getAngleMeasurement())
35                 .setAltitude(getAngleMeasurement())
36                 .build();
37     }
38 
getDistanceMeasurement()39     public static DistanceMeasurement getDistanceMeasurement() {
40         return new DistanceMeasurement.Builder()
41                 .setMeters(getDoubleInRange(0, 100))
42                 .setErrorMeters(getDoubleInRange(0, 10))
43                 .setConfidenceLevel(getDoubleInRange(0, 1))
44                 .build();
45     }
46 
getRangingMeasurement()47     public static RangingMeasurement getRangingMeasurement() {
48         return getRangingMeasurement(getUwbAddress(false));
49     }
50 
getRangingMeasurement(UwbAddress address)51     public static RangingMeasurement getRangingMeasurement(UwbAddress address) {
52         return new RangingMeasurement.Builder()
53                 .setDistanceMeasurement(getDistanceMeasurement())
54                 .setAngleOfArrivalMeasurement(getAngleOfArrivalMeasurement())
55                 .setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos())
56                 .setRemoteDeviceAddress(address != null ? address : getUwbAddress(false))
57                 .setStatus(RangingMeasurement.RANGING_STATUS_SUCCESS)
58                 .build();
59     }
60 
getRangingReports(int numMeasurements)61     public static RangingReport getRangingReports(int numMeasurements) {
62         RangingReport.Builder builder = new RangingReport.Builder();
63         for (int i = 0; i < numMeasurements; i++) {
64             builder.addMeasurement(getRangingMeasurement());
65         }
66         return builder.build();
67     }
68 
getDoubleInRange(double min, double max)69     private static double getDoubleInRange(double min, double max) {
70         return min + (max - min) * Math.random();
71     }
72 
getUwbAddress(boolean isShortAddress)73     public static UwbAddress getUwbAddress(boolean isShortAddress) {
74         byte[] addressBytes = new byte[isShortAddress ? UwbAddress.SHORT_ADDRESS_BYTE_LENGTH :
75                 UwbAddress.EXTENDED_ADDRESS_BYTE_LENGTH];
76         for (int i = 0; i < addressBytes.length; i++) {
77             addressBytes[i] = (byte) getDoubleInRange(1, 255);
78         }
79         return UwbAddress.fromBytes(addressBytes);
80     }
81 
getExecutor()82     public static Executor getExecutor() {
83         return new Executor() {
84             @Override
85             public void execute(Runnable command) {
86                 command.run();
87             }
88         };
89     }
90 }
91