1 /*
2  * Copyright (C) 2023 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.google.uwb.support.radar;
18 
19 import android.os.PersistableBundle;
20 import android.uwb.UwbManager;
21 
22 import androidx.annotation.NonNull;
23 
24 import com.google.uwb.support.base.RequiredParam;
25 import com.google.uwb.support.fira.FiraParams.PrfMode;
26 import com.google.uwb.support.fira.FiraParams.RframeConfig;
27 import com.google.uwb.support.fira.FiraParams.UwbChannel;
28 
29 /**
30  * Defines parameters to open a Radar session.
31  *
32  * <p>This is passed as a bundle to the service API {@link UwbManager#openRangingSession}.
33  */
34 public class RadarOpenSessionParams extends RadarParams {
35     private static final int BUNDLE_VERSION_1 = 1;
36     private static final int BUNDLE_VERSION_CURRENT = BUNDLE_VERSION_1;
37 
38     private static final String KEY_SESSION_ID = "session_id";
39     private static final String KEY_SESSION_TYPE = "session_type";
40     private static final String KEY_BURST_PERIOD = "burst_period";
41     private static final String KEY_SWEEP_PERIOD = "sweep_period";
42     private static final String KEY_SWEEPS_PER_BURST = "sweeps_per_burst";
43     private static final String KEY_SAMPLES_PER_SWEEP = "samples_per_sweep";
44     private static final String KEY_CHANNEL_NUMBER = "channel_number";
45     private static final String KEY_SWEEP_OFFSET = "sweep_offset";
46     private static final String KEY_RFRAME_CONFIG = "rframe_config";
47     private static final String KEY_PREAMBLE_DURATION = "preamble_duration";
48     private static final String KEY_PREAMBLE_CODE_INDEX = "preamble_code_index";
49     private static final String KEY_SESSION_PRIORITY = "session_priority";
50     private static final String KEY_BITS_PER_SAMPLE = "bits_per_samples";
51     private static final String KEY_PRF_MODE = "prf_mode";
52     private static final String KEY_NUMBER_OF_BURSTS = "number_of_bursts";
53     private static final String KEY_RADAR_DATA_TYPE = "radar_data_type";
54 
55     private final int mSessionId;
56     @SessionType private final int mSessionType;
57     @BurstPeriod private final int mBurstPeriod;
58     @SweepPeriod private final int mSweepPeriod;
59     @SweepsPerBurst private final int mSweepsPerBurst;
60     @SamplesPerSweep private final int mSamplesPerSweep;
61     @UwbChannel private final int mChannelNumber;
62     @SweepOffset private final int mSweepOffset;
63     @RframeConfig private final int mRframeConfig;
64     @PreambleDuration private final int mPreambleDuration;
65     @PreambleCodeIndex private final int mPreambleCodeIndex;
66     @SessionPriority private final int mSessionPriority;
67     @BitsPerSample private final int mBitsPerSample;
68     @PrfMode private final int mPrfMode;
69     @NumberOfBursts private final int mNumberOfBursts;
70     @RadarDataType private final int mRadarDataType;
71 
RadarOpenSessionParams( int sessionId, @SessionType int sessionType, @BurstPeriod int burstPeriod, @SweepPeriod int sweepPeriod, @SweepsPerBurst int sweepsPerBurst, @SamplesPerSweep int samplesPerSweep, @UwbChannel int channelNumber, @SweepOffset int sweepOffset, @RframeConfig int rframeConfig, @PreambleDuration int preambleDuration, @PreambleCodeIndex int preambleCodeIndex, @SessionPriority int sessionPriority, @BitsPerSample int bitsPerSample, @PrfMode int prfMode, @NumberOfBursts int numberOfBursts, @RadarDataType int radarDataType)72     private RadarOpenSessionParams(
73             int sessionId,
74             @SessionType int sessionType,
75             @BurstPeriod int burstPeriod,
76             @SweepPeriod int sweepPeriod,
77             @SweepsPerBurst int sweepsPerBurst,
78             @SamplesPerSweep int samplesPerSweep,
79             @UwbChannel int channelNumber,
80             @SweepOffset int sweepOffset,
81             @RframeConfig int rframeConfig,
82             @PreambleDuration int preambleDuration,
83             @PreambleCodeIndex int preambleCodeIndex,
84             @SessionPriority int sessionPriority,
85             @BitsPerSample int bitsPerSample,
86             @PrfMode int prfMode,
87             @NumberOfBursts int numberOfBursts,
88             @RadarDataType int radarDataType) {
89         mSessionId = sessionId;
90         mSessionType = sessionType;
91         mBurstPeriod = burstPeriod;
92         mSweepPeriod = sweepPeriod;
93         mSweepsPerBurst = sweepsPerBurst;
94         mSamplesPerSweep = samplesPerSweep;
95         mChannelNumber = channelNumber;
96         mSweepOffset = sweepOffset;
97         mRframeConfig = rframeConfig;
98         mPreambleDuration = preambleDuration;
99         mPreambleCodeIndex = preambleCodeIndex;
100         mSessionPriority = sessionPriority;
101         mBitsPerSample = bitsPerSample;
102         mPrfMode = prfMode;
103         mNumberOfBursts = numberOfBursts;
104         mRadarDataType = radarDataType;
105     }
106 
107     @Override
getBundleVersion()108     protected int getBundleVersion() {
109         return BUNDLE_VERSION_CURRENT;
110     }
111 
112     @Override
toBundle()113     public PersistableBundle toBundle() {
114         PersistableBundle bundle = super.toBundle();
115         bundle.putInt(KEY_SESSION_ID, mSessionId);
116         bundle.putInt(KEY_SESSION_TYPE, mSessionType);
117         bundle.putInt(KEY_BURST_PERIOD, mBurstPeriod);
118         bundle.putInt(KEY_SWEEP_PERIOD, mSweepPeriod);
119         bundle.putInt(KEY_SWEEPS_PER_BURST, mSweepsPerBurst);
120         bundle.putInt(KEY_SAMPLES_PER_SWEEP, mSamplesPerSweep);
121         bundle.putInt(KEY_CHANNEL_NUMBER, mChannelNumber);
122         bundle.putInt(KEY_SWEEP_OFFSET, mSweepOffset);
123         bundle.putInt(KEY_RFRAME_CONFIG, mRframeConfig);
124         bundle.putInt(KEY_PREAMBLE_DURATION, mPreambleDuration);
125         bundle.putInt(KEY_PREAMBLE_CODE_INDEX, mPreambleCodeIndex);
126         bundle.putInt(KEY_SESSION_PRIORITY, mSessionPriority);
127         bundle.putInt(KEY_BITS_PER_SAMPLE, mBitsPerSample);
128         bundle.putInt(KEY_PRF_MODE, mPrfMode);
129         bundle.putInt(KEY_NUMBER_OF_BURSTS, mNumberOfBursts);
130         bundle.putInt(KEY_RADAR_DATA_TYPE, mRadarDataType);
131         return bundle;
132     }
133 
134     /** Unpack the {@link PersistableBundle} to a {@link RadarOpenSessionParams} */
fromBundle(PersistableBundle bundle)135     public static RadarOpenSessionParams fromBundle(PersistableBundle bundle) {
136         if (!isCorrectProtocol(bundle)) {
137             throw new IllegalArgumentException("Invalid protocol");
138         }
139 
140         switch (getBundleVersion(bundle)) {
141             case BUNDLE_VERSION_1:
142                 return parseBundleVersion1(bundle);
143 
144             default:
145                 throw new IllegalArgumentException("unknown bundle version");
146         }
147     }
148 
parseBundleVersion1(PersistableBundle bundle)149     private static RadarOpenSessionParams parseBundleVersion1(PersistableBundle bundle) {
150         return new Builder()
151                 .setSessionId(bundle.getInt(KEY_SESSION_ID))
152                 .setBurstPeriod(bundle.getInt(KEY_BURST_PERIOD))
153                 .setSweepPeriod(bundle.getInt(KEY_SWEEP_PERIOD))
154                 .setSweepsPerBurst(bundle.getInt(KEY_SWEEPS_PER_BURST))
155                 .setSamplesPerSweep(bundle.getInt(KEY_SAMPLES_PER_SWEEP))
156                 .setChannelNumber(bundle.getInt(KEY_CHANNEL_NUMBER))
157                 .setSweepOffset(bundle.getInt(KEY_SWEEP_OFFSET))
158                 .setRframeConfig(bundle.getInt(KEY_RFRAME_CONFIG))
159                 .setPreambleDuration(bundle.getInt(KEY_PREAMBLE_DURATION))
160                 .setPreambleCodeIndex(bundle.getInt(KEY_PREAMBLE_CODE_INDEX))
161                 .setSessionPriority(bundle.getInt(KEY_SESSION_PRIORITY))
162                 .setBitsPerSample(bundle.getInt(KEY_BITS_PER_SAMPLE))
163                 .setPrfMode(bundle.getInt(KEY_PRF_MODE))
164                 .setNumberOfBursts(bundle.getInt(KEY_NUMBER_OF_BURSTS))
165                 .setRadarDataType(bundle.getInt(KEY_RADAR_DATA_TYPE))
166                 .build();
167     }
168 
getSessionId()169     public int getSessionId() {
170         return mSessionId;
171     }
172 
173     @SessionType
getSessionType()174     public int getSessionType() {
175         return mSessionType;
176     }
177 
178     @BurstPeriod
getBurstPeriod()179     public int getBurstPeriod() {
180         return mBurstPeriod;
181     }
182 
183     @SweepPeriod
getSweepPeriod()184     public int getSweepPeriod() {
185         return mSweepPeriod;
186     }
187 
188     @SweepsPerBurst
getSweepsPerBurst()189     public int getSweepsPerBurst() {
190         return mSweepsPerBurst;
191     }
192 
193     @SamplesPerSweep
getSamplesPerSweep()194     public int getSamplesPerSweep() {
195         return mSamplesPerSweep;
196     }
197 
198     @UwbChannel
getChannelNumber()199     public int getChannelNumber() {
200         return mChannelNumber;
201     }
202 
203     @SweepOffset
getSweepOffset()204     public int getSweepOffset() {
205         return mSweepOffset;
206     }
207 
208     @RframeConfig
getRframeConfig()209     public int getRframeConfig() {
210         return mRframeConfig;
211     }
212 
213     @PreambleDuration
getPreambleDuration()214     public int getPreambleDuration() {
215         return mPreambleDuration;
216     }
217 
218     @PreambleCodeIndex
getPreambleCodeIndex()219     public int getPreambleCodeIndex() {
220         return mPreambleCodeIndex;
221     }
222 
223     @SessionPriority
getSessionPriority()224     public int getSessionPriority() {
225         return mSessionPriority;
226     }
227 
228     @BitsPerSample
getBitsPerSample()229     public int getBitsPerSample() {
230         return mBitsPerSample;
231     }
232 
233     @PrfMode
getPrfMode()234     public int getPrfMode() {
235         return mPrfMode;
236     }
237 
238     @NumberOfBursts
getNumberOfBursts()239     public int getNumberOfBursts() {
240         return mNumberOfBursts;
241     }
242 
243     @RadarDataType
getRadarDataType()244     public int getRadarDataType() {
245         return mRadarDataType;
246     }
247 
248     /** Builder */
249     public static final class Builder {
250         private RequiredParam<Integer> mSessionId = new RequiredParam<>();
251         @SessionType private int mSessionType = RadarParams.SESSION_TYPE_RADAR;
252         @BurstPeriod private RequiredParam<Integer> mBurstPeriod = new RequiredParam<>();
253         @SweepPeriod private RequiredParam<Integer> mSweepPeriod = new RequiredParam<>();
254         @SweepsPerBurst private RequiredParam<Integer> mSweepsPerBurst = new RequiredParam<>();
255         @SamplesPerSweep private RequiredParam<Integer> mSamplesPerSweep = new RequiredParam<>();
256         @UwbChannel private RequiredParam<Integer> mChannelNumber = new RequiredParam<>();
257         @SweepOffset private RequiredParam<Integer> mSweepOffset = new RequiredParam<>();
258         @RframeConfig private RequiredParam<Integer> mRframeConfig = new RequiredParam<>();
259         @PreambleDuration private RequiredParam<Integer> mPreambleDuration = new RequiredParam<>();
260 
261         @PreambleCodeIndex
262         private RequiredParam<Integer> mPreambleCodeIndex = new RequiredParam<>();
263 
264         @SessionPriority private RequiredParam<Integer> mSessionPriority = new RequiredParam<>();
265         @BitsPerSample private RequiredParam<Integer> mBitsPerSample = new RequiredParam<>();
266         @PrfMode private RequiredParam<Integer> mPrfMode = new RequiredParam<>();
267         @NumberOfBursts private RequiredParam<Integer> mNumberOfBursts = new RequiredParam<>();
268         @RadarDataType private RequiredParam<Integer> mRadarDataType = new RequiredParam<>();
269 
Builder()270         public Builder() {}
271 
Builder(@onNull Builder builder)272         public Builder(@NonNull Builder builder) {
273             mSessionId.set(builder.mSessionId.get());
274             mSessionType = builder.mSessionType;
275             mBurstPeriod.set(builder.mBurstPeriod.get());
276             mSweepPeriod.set(builder.mSweepPeriod.get());
277             mSweepsPerBurst.set(builder.mSweepsPerBurst.get());
278             mSamplesPerSweep.set(builder.mSamplesPerSweep.get());
279             mChannelNumber.set(builder.mChannelNumber.get());
280             mSweepOffset.set(builder.mSweepOffset.get());
281             mRframeConfig.set(builder.mRframeConfig.get());
282             mPreambleDuration.set(builder.mPreambleDuration.get());
283             mPreambleCodeIndex.set(builder.mPreambleCodeIndex.get());
284             mSessionPriority.set(builder.mSessionPriority.get());
285             mBitsPerSample.set(builder.mBitsPerSample.get());
286             mPrfMode.set(builder.mPrfMode.get());
287             mNumberOfBursts.set(builder.mNumberOfBursts.get());
288             mRadarDataType.set(builder.mRadarDataType.get());
289         }
290 
Builder(@onNull RadarOpenSessionParams params)291         public Builder(@NonNull RadarOpenSessionParams params) {
292             mSessionId.set(params.mSessionId);
293             mSessionType = params.mSessionType;
294             mBurstPeriod.set(params.mBurstPeriod);
295             mSweepPeriod.set(params.mSweepPeriod);
296             mSweepsPerBurst.set(params.mSweepsPerBurst);
297             mSamplesPerSweep.set(params.mSamplesPerSweep);
298             mChannelNumber.set(params.mChannelNumber);
299             mSweepOffset.set(params.mSweepOffset);
300             mRframeConfig.set(params.mRframeConfig);
301             mPreambleDuration.set(params.mPreambleDuration);
302             mPreambleCodeIndex.set(params.mPreambleCodeIndex);
303             mSessionPriority.set(params.mSessionPriority);
304             mBitsPerSample.set(params.mBitsPerSample);
305             mPrfMode.set(params.mPrfMode);
306             mNumberOfBursts.set(params.mNumberOfBursts);
307             mRadarDataType.set(params.mRadarDataType);
308         }
309 
310         /** Sets session id */
setSessionId(int sessionId)311         public Builder setSessionId(int sessionId) {
312             mSessionId.set(sessionId);
313             return this;
314         }
315 
316         /** Sets burst period */
setBurstPeriod(@urstPeriod int burstPeriod)317         public Builder setBurstPeriod(@BurstPeriod int burstPeriod) {
318             mBurstPeriod.set(burstPeriod);
319             return this;
320         }
321 
322         /** Sets sweep period */
setSweepPeriod(@weepPeriod int sweepPeriod)323         public Builder setSweepPeriod(@SweepPeriod int sweepPeriod) {
324             mSweepPeriod.set(sweepPeriod);
325             return this;
326         }
327 
328         /** Sets sweeps per burst */
setSweepsPerBurst(@weepsPerBurst int sweepsPerBurst)329         public Builder setSweepsPerBurst(@SweepsPerBurst int sweepsPerBurst) {
330             mSweepsPerBurst.set(sweepsPerBurst);
331             return this;
332         }
333 
334         /** Sets samples per sweep */
setSamplesPerSweep(@amplesPerSweep int samplesPerSweep)335         public Builder setSamplesPerSweep(@SamplesPerSweep int samplesPerSweep) {
336             mSamplesPerSweep.set(samplesPerSweep);
337             return this;
338         }
339 
340         /** Sets channel number */
setChannelNumber(@wbChannel int channelNumber)341         public Builder setChannelNumber(@UwbChannel int channelNumber) {
342             mChannelNumber.set(channelNumber);
343             return this;
344         }
345 
346         /** Sets sweep offset */
setSweepOffset(@weepOffset int sweepOffset)347         public Builder setSweepOffset(@SweepOffset int sweepOffset) {
348             mSweepOffset.set(sweepOffset);
349             return this;
350         }
351 
352         /** Sets rframe config */
setRframeConfig(@frameConfig int rframeConfig)353         public Builder setRframeConfig(@RframeConfig int rframeConfig) {
354             mRframeConfig.set(rframeConfig);
355             return this;
356         }
357 
358         /** Sets preamble duration */
setPreambleDuration(@reambleDuration int preambleDuration)359         public Builder setPreambleDuration(@PreambleDuration int preambleDuration) {
360             mPreambleDuration.set(preambleDuration);
361             return this;
362         }
363 
364         /** Sets preamble code index */
setPreambleCodeIndex(@reambleCodeIndex int preambleCodeIndex)365         public Builder setPreambleCodeIndex(@PreambleCodeIndex int preambleCodeIndex) {
366             mPreambleCodeIndex.set(preambleCodeIndex);
367             return this;
368         }
369 
370         /** Sets session priority */
setSessionPriority(@essionPriority int sessionPriority)371         public Builder setSessionPriority(@SessionPriority int sessionPriority) {
372             mSessionPriority.set(sessionPriority);
373             return this;
374         }
375 
376         /** Sets bits per sample */
setBitsPerSample(@itsPerSample int bitsPerSample)377         public Builder setBitsPerSample(@BitsPerSample int bitsPerSample) {
378             mBitsPerSample.set(bitsPerSample);
379             return this;
380         }
381 
382         /** Sets PRF mode */
setPrfMode(@rfMode int prfMode)383         public Builder setPrfMode(@PrfMode int prfMode) {
384             mPrfMode.set(prfMode);
385             return this;
386         }
387 
388         /** Sets number of bursts */
setNumberOfBursts(@umberOfBursts int numberOfBursts)389         public Builder setNumberOfBursts(@NumberOfBursts int numberOfBursts) {
390             mNumberOfBursts.set(numberOfBursts);
391             return this;
392         }
393 
394         /** Sets radar data type */
setRadarDataType(@adarDataType int radarDataType)395         public Builder setRadarDataType(@RadarDataType int radarDataType) {
396             mRadarDataType.set(radarDataType);
397             return this;
398         }
399 
400         /** Build {@link RadarOpenSessionParams} */
build()401         public RadarOpenSessionParams build() {
402             return new RadarOpenSessionParams(
403                     mSessionId.get(),
404                     mSessionType,
405                     mBurstPeriod.get(),
406                     mSweepPeriod.get(),
407                     mSweepsPerBurst.get(),
408                     mSamplesPerSweep.get(),
409                     mChannelNumber.get(),
410                     mSweepOffset.get(),
411                     mRframeConfig.get(),
412                     mPreambleDuration.get(),
413                     mPreambleCodeIndex.get(),
414                     mSessionPriority.get(),
415                     mBitsPerSample.get(),
416                     mPrfMode.get(),
417                     mNumberOfBursts.get(),
418                     mRadarDataType.get());
419         }
420     }
421 }
422