• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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.ranging.rtt.backend;
18 
19 import androidx.annotation.IntDef;
20 import androidx.annotation.NonNull;
21 
22 import java.lang.annotation.Retention;
23 import java.lang.annotation.RetentionPolicy;
24 import java.util.Arrays;
25 
26 public class RttRangingParameters {
27 
28     @Retention(RetentionPolicy.SOURCE)
29     @IntDef({
30             DeviceRole.PUBLISHER,
31             DeviceRole.SUBSCRIBER,
32     })
33     public @interface DeviceRole {
34         int PUBLISHER = 0;
35         int SUBSCRIBER = 1;
36     }
37 
38     @IntDef({
39             INFREQUENT,
40             NORMAL,
41             FAST,
42     })
43     public @interface RangingUpdateRate {}
44 
45     /**
46      * Requests for ranging data in 512 milliseconds
47      */
48     public static final int NORMAL = 1;
49 
50     /**
51      * Requests for ranging data in 8192 milliseconds
52      */
53     public static final int INFREQUENT = 2;
54 
55     /**
56      * Requests for ranging data in 256 milliseconds
57      */
58     public static final int FAST = 3;
59 
60     private final @DeviceRole int mDeviceRole;
61     /**
62      * Returns Service ID for WiFi Aware
63      */
64     protected final byte mServiceId;
65     protected final byte[] mMatchFilter;
66     protected final String mServiceName;
67     protected final int mMaxDistanceMm;
68     protected final int mMinDistanceMm;
69 
70     @RangingUpdateRate
71     private final int mUpdateRate;
72 
73     private final boolean mEnablePeriodicRangingHwFeature;
74 
75     private final boolean mRangeDataNtfDisabled;
76 
getDeviceRole()77     public int getDeviceRole() {
78         return mDeviceRole;
79     }
80 
81     /**
82      * get Service ID of the RTT session
83      *
84      * @return Service ID of the RTT session
85      */
getServiceId()86     public byte getServiceId() {
87         return mServiceId;
88     }
89 
90     /**
91      * get Service name of the RTT session
92      *
93      * @return Service name of the RTT session
94      */
getServiceName()95     public String getServiceName() {
96         return mServiceName;
97     }
98 
99     /**
100      * get Match filter bytes of the RTT session
101      *
102      * @return Match filter bytes of the RTT session
103      */
getMatchFilter()104     public byte[] getMatchFilter() {
105         return mMatchFilter;
106     }
107 
108     /**
109      * get Max Distance limit of the RTT session(Unit : mm)
110      *
111      * @return Max Distance limit of the RTT session(Unit : mm)
112      */
getMaxDistanceMm()113     public int getMaxDistanceMm() {
114         return mMaxDistanceMm;
115     }
116 
117     /**
118      * get Min Distance limit of the RTT session(Unit : mm)
119      *
120      * @return Min Distance limit of the RTT session(Unit : mm)
121      */
getMinDistanceMm()122     public int getMinDistanceMm() {
123         return mMinDistanceMm;
124     }
125 
getUpdateRate()126     public int getUpdateRate() {
127         return mUpdateRate;
128     }
129 
isPeriodicRangingHwFeatureEnabled()130     public boolean isPeriodicRangingHwFeatureEnabled() {
131         return mEnablePeriodicRangingHwFeature;
132     }
133 
isRangeDataNtfDisabled()134     public boolean isRangeDataNtfDisabled() {
135         return mRangeDataNtfDisabled;
136     }
137 
RttRangingParameters(Builder builder)138     public RttRangingParameters(Builder builder) {
139         mDeviceRole = builder.mDeviceRole;
140         mServiceId = builder.mServiceId;
141         mServiceName = builder.mServiceName;
142         mMatchFilter = builder.mMatchFilter;
143         mMaxDistanceMm = builder.mMaxDistanceMm;
144         mMinDistanceMm = builder.mMinDistanceMm;
145         mUpdateRate = builder.mRangingUpdateRate;
146         mEnablePeriodicRangingHwFeature = builder.mEnablePeriodicRangingHwFeature;
147         mRangeDataNtfDisabled = builder.mRangeDataNtfDisabled;
148     }
149 
150 
151     /**
152      * Returns a builder for {@link com.android.ranging.generic.ranging.rtt.RttRangingParameters}.
153      */
154     public static class Builder {
155         protected @DeviceRole int mDeviceRole = DeviceRole.PUBLISHER;
156         protected byte mServiceId = 0;
157         protected String mServiceName = "";
158         protected byte[] mMatchFilter = new byte[]{};
159         protected int mMaxDistanceMm = 30 * 100 * 100;
160         protected int mMinDistanceMm = 0;
161         private int mRangingUpdateRate = NORMAL;
162         private boolean mEnablePeriodicRangingHwFeature = false;
163         private boolean mRangeDataNtfDisabled = false;
164 
setDeviceRole(int deviceRole)165         public Builder setDeviceRole(int deviceRole) {
166             mDeviceRole = deviceRole;
167             return this;
168         }
169 
170         /** Set the service ID that produced this data */
setServiceId(byte serviceId)171         public Builder setServiceId(byte serviceId) {
172             mServiceId = serviceId;
173             return this;
174         }
175 
setServiceName(String serviceName)176         public Builder setServiceName(String serviceName) {
177             mServiceName = serviceName;
178             return this;
179         }
180 
setMaxDistanceMm(int maxDistanceMm)181         public Builder setMaxDistanceMm(int maxDistanceMm) {
182             mMaxDistanceMm = maxDistanceMm;
183             return this;
184         }
185 
setMinDistanceMm(int minDistanceMm)186         public Builder setMinDistanceMm(int minDistanceMm) {
187             mMinDistanceMm = minDistanceMm;
188             return this;
189         }
190 
setMatchFilter(byte[] matchFilter)191         public Builder setMatchFilter(byte[] matchFilter) {
192             mMatchFilter = matchFilter;
193             return this;
194         }
195 
setUpdateRate(int updateRate)196         public Builder setUpdateRate(int updateRate) {
197             mRangingUpdateRate = updateRate;
198             return this;
199         }
200 
setPeriodicRangingHwFeatureEnabled(boolean enabled)201         public Builder setPeriodicRangingHwFeatureEnabled(boolean enabled) {
202             mEnablePeriodicRangingHwFeature = enabled;
203             return this;
204         }
205 
setRangeDataNtfDisabled(boolean rangeDataNtfDisabled)206         public Builder setRangeDataNtfDisabled(boolean rangeDataNtfDisabled) {
207             mRangeDataNtfDisabled = rangeDataNtfDisabled;
208             return this;
209         }
210 
build()211         public RttRangingParameters build() {
212             return new RttRangingParameters(this);
213         }
214     }
215 
getIntervalMs(@onNull RttRangingParameters rttRangingParameters)216     public static int getIntervalMs(@NonNull RttRangingParameters rttRangingParameters) {
217         switch (rttRangingParameters.getUpdateRate()) {
218             case FAST -> {
219                 return rttRangingParameters.isPeriodicRangingHwFeatureEnabled() ? 128 : 256;
220             }
221             case INFREQUENT -> {
222                 return 8192;
223             }
224             default -> {
225                 return rttRangingParameters.isPeriodicRangingHwFeatureEnabled() ? 256 : 512;
226             }
227         }
228     }
229 
230     @Override
toString()231     public String toString() {
232         return "RttRangingParameters{ "
233                 + "deviceRole: "
234                 + mDeviceRole
235                 + ", serviceId: "
236                 + mServiceId
237                 + ", serviceName: "
238                 + mServiceName
239                 + ", matchFilter: "
240                 + Arrays.toString(mMatchFilter)
241                 + ", maxDistanceMm: "
242                 + mMaxDistanceMm
243                 + ", minDistanceMm: "
244                 + mMinDistanceMm
245                 + ", enablePeriodicRangingHwFeature: "
246                 + mEnablePeriodicRangingHwFeature
247                 + ", rangeDataNtfDisabled: "
248                 + mRangeDataNtfDisabled
249                 + " }";
250     }
251 }
252