1 /* 2 * Copyright (C) 2017 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.wifi.rtt; 18 19 import android.hardware.wifi.V1_0.RttResult; 20 import android.hardware.wifi.V1_0.RttStatus; 21 import android.net.MacAddress; 22 import android.net.wifi.ScanResult; 23 import android.net.wifi.rtt.RangingRequest; 24 import android.net.wifi.rtt.RangingResult; 25 import android.net.wifi.rtt.ResponderConfig; 26 import android.util.Pair; 27 28 import java.util.ArrayList; 29 import java.util.List; 30 31 /** 32 * Utilities for the Rtt unit test suite. 33 */ 34 public class RttTestUtils { 35 /** 36 * Compare the two lists and return true for equality, false otherwise. The two lists are 37 * considered identical if they have the same number of elements and contain equal elements 38 * (equality of elements using the equal() operator of the component objects). 39 * 40 * Note: null != empty list 41 */ 42 public static boolean compareListContentsNoOrdering(List a, List b) { 43 if (a == b) { 44 return true; 45 } 46 if (a == null || b == null) { 47 return false; // at this point they're not both null 48 } 49 if (a.size() != b.size()) { // at this point neither is null 50 return false; 51 } 52 return a.containsAll(b) && b.containsAll(a); 53 } 54 55 /** 56 * Returns a dummy ranging request with 3 requests: 57 * - First: 802.11mc capable 58 * - Second: 802.11mc not capable 59 * - Third: Aware peer 60 */ 61 public static RangingRequest getDummyRangingRequest(byte lastMacByte) { 62 RangingRequest.Builder builder = new RangingRequest.Builder(); 63 64 ScanResult scan1 = new ScanResult(); 65 scan1.BSSID = "00:01:02:03:04:" + String.format("%02d", lastMacByte); 66 scan1.setFlag(ScanResult.FLAG_80211mc_RESPONDER); 67 scan1.channelWidth = ScanResult.CHANNEL_WIDTH_40MHZ; 68 ScanResult scan2 = new ScanResult(); 69 scan2.BSSID = "0A:0B:0C:0D:0E:" + String.format("%02d", lastMacByte); 70 scan2.channelWidth = ScanResult.CHANNEL_WIDTH_20MHZ; 71 MacAddress mac1 = MacAddress.fromString("08:09:08:07:06:05"); 72 73 builder.addAccessPoint(scan1); 74 builder.addAccessPoint(scan2); 75 builder.addWifiAwarePeer(mac1); 76 77 return builder.build(); 78 } 79 80 /** 81 * Returns a dummy ranging request with 2 requests - neither of which support 802.11mc. 82 */ 83 public static RangingRequest getDummyRangingRequestNo80211mcSupport(byte lastMacByte) { 84 RangingRequest.Builder builder = new RangingRequest.Builder(); 85 86 ScanResult scan1 = new ScanResult(); 87 scan1.BSSID = "00:01:02:03:04:" + String.format("%02d", lastMacByte); 88 ScanResult scan2 = new ScanResult(); 89 scan2.BSSID = "0A:0B:0C:0D:0E:" + String.format("%02d", lastMacByte); 90 91 builder.addAccessPoint(scan1); 92 builder.addAccessPoint(scan2); 93 94 return builder.build(); 95 } 96 97 /** 98 * Returns a matched set of dummy ranging results: HAL RttResult and the public API 99 * RangingResult. 100 * 101 * @param request If non-null will be used as a template (BSSID) for the range results. 102 */ 103 public static Pair<List<RttResult>, List<RangingResult>> getDummyRangingResults( 104 RangingRequest request) { 105 int rangeCmBase = 15; 106 int rangeStdDevCmBase = 3; 107 int rssiBase = -20; 108 long rangeTimestampBase = 666; 109 List<RttResult> halResults = new ArrayList<>(); 110 List<RangingResult> results = new ArrayList<>(); 111 112 if (request != null) { 113 for (ResponderConfig peer: request.mRttPeers) { 114 RangingResult rangingResult; 115 if (peer.peerHandle == null) { 116 rangingResult = new RangingResult(RangingResult.STATUS_SUCCESS, 117 peer.macAddress, rangeCmBase++, rangeStdDevCmBase++, rssiBase++, 118 8, 5, null, null, rangeTimestampBase++); 119 } else { 120 rangingResult = new RangingResult(RangingResult.STATUS_SUCCESS, 121 peer.peerHandle, rangeCmBase++, rangeStdDevCmBase++, rssiBase++, 122 8, 5, null, null, rangeTimestampBase++); 123 } 124 results.add(rangingResult); 125 halResults.add(getMatchingRttResult(rangingResult, peer.macAddress)); 126 } 127 } else { 128 results.add(new RangingResult(RangingResult.STATUS_SUCCESS, 129 MacAddress.fromString("10:01:02:03:04:05"), rangeCmBase++, 130 rangeStdDevCmBase++, rssiBase++, 8, 4, null, null, rangeTimestampBase++)); 131 results.add(new RangingResult(RangingResult.STATUS_SUCCESS, 132 MacAddress.fromString("1A:0B:0C:0D:0E:0F"), rangeCmBase++, 133 rangeStdDevCmBase++, rssiBase++, 9, 3, null, null, rangeTimestampBase++)); 134 results.add(new RangingResult(RangingResult.STATUS_SUCCESS, 135 MacAddress.fromString("08:09:08:07:06:05"), rangeCmBase++, 136 rangeStdDevCmBase++, rssiBase++, 10, 2, null, null, rangeTimestampBase++)); 137 halResults.add(getMatchingRttResult(results.get(0), null)); 138 halResults.add(getMatchingRttResult(results.get(1), null)); 139 halResults.add(getMatchingRttResult(results.get(2), null)); 140 } 141 142 return new Pair<>(halResults, results); 143 } 144 145 private static RttResult getMatchingRttResult(RangingResult rangingResult, 146 MacAddress overrideMac) { 147 RttResult rttResult = new RttResult(); 148 rttResult.status = rangingResult.getStatus() == RangingResult.STATUS_SUCCESS 149 ? RttStatus.SUCCESS : RttStatus.FAILURE; 150 System.arraycopy(overrideMac == null ? rangingResult.getMacAddress().toByteArray() 151 : overrideMac.toByteArray(), 0, rttResult.addr, 0, 6); 152 rttResult.distanceInMm = rangingResult.getDistanceMm(); 153 rttResult.distanceSdInMm = rangingResult.getDistanceStdDevMm(); 154 rttResult.rssi = rangingResult.getRssi() * -2; 155 rttResult.numberPerBurstPeer = (byte) rangingResult.getNumAttemptedMeasurements(); 156 rttResult.successNumber = rangingResult.getNumSuccessfulMeasurements(); 157 rttResult.timeStampInUs = rangingResult.getRangingTimestampMillis() * 1000; 158 159 return rttResult; 160 } 161 } 162