1 /*
2  * Copyright 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 android.uwb.cts;
18 
19 import android.content.Context;
20 import android.content.pm.PackageManager;
21 import android.os.SystemClock;
22 import android.uwb.AngleMeasurement;
23 import android.uwb.AngleOfArrivalMeasurement;
24 import android.uwb.DistanceMeasurement;
25 import android.uwb.RangingMeasurement;
26 import android.uwb.RangingReport;
27 import android.uwb.UwbAddress;
28 
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.concurrent.Executor;
32 
33 public class UwbTestUtils {
UwbTestUtils()34     private UwbTestUtils() {}
35 
isUwbSupported(Context context)36     public static boolean isUwbSupported(Context context) {
37         PackageManager packageManager = context.getPackageManager();
38         return packageManager.hasSystemFeature(PackageManager.FEATURE_UWB);
39     }
40 
getAngleMeasurement()41     public static AngleMeasurement getAngleMeasurement() {
42         return new AngleMeasurement(
43                 getDoubleInRange(-Math.PI, Math.PI),
44                 getDoubleInRange(0, Math.PI),
45                 getDoubleInRange(0, 1));
46     }
47 
getAngleOfArrivalMeasurement()48     public static AngleOfArrivalMeasurement getAngleOfArrivalMeasurement() {
49         return new AngleOfArrivalMeasurement.Builder(getAngleMeasurement())
50                 .setAltitude(getAngleMeasurement())
51                 .build();
52     }
53 
getDistanceMeasurement()54     public static DistanceMeasurement getDistanceMeasurement() {
55         return new DistanceMeasurement.Builder()
56                 .setMeters(getDoubleInRange(0, 100))
57                 .setErrorMeters(getDoubleInRange(0, 10))
58                 .setConfidenceLevel(getDoubleInRange(0, 1))
59                 .build();
60     }
61 
getRangingMeasurement()62     public static RangingMeasurement getRangingMeasurement() {
63         return getRangingMeasurement(getUwbAddress(false));
64     }
65 
getRangingMeasurement(UwbAddress address)66     public static RangingMeasurement getRangingMeasurement(UwbAddress address) {
67         return new RangingMeasurement.Builder()
68                 .setDistanceMeasurement(getDistanceMeasurement())
69                 .setAngleOfArrivalMeasurement(getAngleOfArrivalMeasurement())
70                 .setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos())
71                 .setRemoteDeviceAddress(address != null ? address : getUwbAddress(false))
72                 .setStatus(RangingMeasurement.RANGING_STATUS_SUCCESS)
73                 .build();
74     }
75 
getRangingMeasurements(int num)76     public static List<RangingMeasurement> getRangingMeasurements(int num) {
77         List<RangingMeasurement> result = new ArrayList<>();
78         for (int i = 0; i < num; i++) {
79             result.add(getRangingMeasurement());
80         }
81         return result;
82     }
83 
getRangingReports(int numMeasurements)84     public static RangingReport getRangingReports(int numMeasurements) {
85         RangingReport.Builder builder = new RangingReport.Builder();
86         for (int i = 0; i < numMeasurements; i++) {
87             builder.addMeasurement(getRangingMeasurement());
88         }
89         return builder.build();
90     }
91 
getDoubleInRange(double min, double max)92     private static double getDoubleInRange(double min, double max) {
93         return min + (max - min) * Math.random();
94     }
95 
getUwbAddress(boolean isShortAddress)96     public static UwbAddress getUwbAddress(boolean isShortAddress) {
97         byte[] addressBytes = new byte[isShortAddress ? UwbAddress.SHORT_ADDRESS_BYTE_LENGTH :
98                 UwbAddress.EXTENDED_ADDRESS_BYTE_LENGTH];
99         for (int i = 0; i < addressBytes.length; i++) {
100             addressBytes[i] = (byte) getDoubleInRange(1, 255);
101         }
102         return UwbAddress.fromBytes(addressBytes);
103     }
104 
getExecutor()105     public static Executor getExecutor() {
106         return new Executor() {
107             @Override
108             public void execute(Runnable command) {
109                 command.run();
110             }
111         };
112     }
113 }
114