1 /*
2  * Copyright (C) 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.cts.verifier.presence.nan;
18 
19 import android.content.Context;
20 import android.net.wifi.aware.PeerHandle;
21 import android.net.wifi.rtt.RangingRequest;
22 import android.net.wifi.rtt.RangingResult;
23 import android.net.wifi.rtt.RangingResultCallback;
24 import android.net.wifi.rtt.WifiRttManager;
25 import android.util.Log;
26 
27 import java.util.List;
28 import java.util.concurrent.Executor;
29 
30 /** Ranges to a given WiFi Aware Peer handle. */
31 public class RttRanger {
32     private static final String TAG = RttRanger.class.getName();
33 
34     private final WifiRttManager wifiRttManager;
35     private final Executor executor;
36 
37     private NanResultListener nanResultListener;
38     private PeerHandle lastPeerHandle;
39 
RttRanger(Context context, Executor executor)40     public RttRanger(Context context, Executor executor) {
41         this.executor = executor;
42         this.wifiRttManager = context.getSystemService(WifiRttManager.class);
43     }
44 
startRanging(PeerHandle peerHandle, NanResultListener nanResultListener)45     public void startRanging(PeerHandle peerHandle, NanResultListener nanResultListener) {
46         if (!wifiRttManager.isAvailable()) {
47             Log.w(TAG, "WifiRttManager is not available");
48             return;
49         }
50 
51         this.lastPeerHandle = peerHandle;
52         this.nanResultListener = nanResultListener;
53         wifiRttManager.startRanging(
54                 new RangingRequest.Builder().addWifiAwarePeer(peerHandle).build(),
55                 executor,
56                 createRangingResultCallback());
57     }
58 
createRangingResultCallback()59     private RangingResultCallback createRangingResultCallback() {
60         return new RangingResultCallback() {
61             @Override
62             public void onRangingFailure(int code) {
63                 Log.w(TAG, "RTT NAN ranging failed: " + code);
64             }
65 
66             @Override
67             public void onRangingResults(List<RangingResult> results) {
68                 Log.i(TAG, "RTT NAN ranging results: " + results);
69 
70                 if (results.isEmpty()) {
71                     Log.i(TAG, "Range results are empty");
72                     return;
73                 }
74 
75                 if (results.get(0).getStatus() == RangingResult.STATUS_FAIL) {
76                     Log.w(TAG, "Failed to range");
77                 } else {
78                     if (nanResultListener != null) {
79                         RangingResult result = results.get(0);
80                         Log.i(TAG,
81                                 "peerHandle=" + result.getPeerHandle() + ", distMm="
82                                         + result.getDistanceMm());
83                         nanResultListener.onNanResult(result);
84                     }
85                 }
86 
87                 startRanging(lastPeerHandle, nanResultListener);
88             }
89         };
90     }
91 
92     /** Listener for range results. */
93     public interface NanResultListener {
94         void onNanResult(RangingResult result);
95     }
96 }
97