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.android.server.uwb.params;
18 
19 import static com.android.server.uwb.config.ConfigParam.BITS_PER_SAMPLE_KEY;
20 import static com.android.server.uwb.config.ConfigParam.CHANNEL_NUMBER_KEY;
21 import static com.android.server.uwb.config.ConfigParam.NUMBER_OF_BURSTS_KEY;
22 import static com.android.server.uwb.config.ConfigParam.PREAMBLE_CODE_INDEX_KEY;
23 import static com.android.server.uwb.config.ConfigParam.PREAMBLE_DURATION_KEY;
24 import static com.android.server.uwb.config.ConfigParam.PRF_MODE_KEY;
25 import static com.android.server.uwb.config.ConfigParam.RADAR_DATA_TYPE_KEY;
26 import static com.android.server.uwb.config.ConfigParam.RADAR_TIMING_PARAMS_KEY;
27 import static com.android.server.uwb.config.ConfigParam.RFRAME_CONFIG_KEY;
28 import static com.android.server.uwb.config.ConfigParam.SAMPLES_PER_SWEEP_KEY;
29 import static com.android.server.uwb.config.ConfigParam.SESSION_PRIORITY_KEY;
30 import static com.android.server.uwb.config.ConfigParam.SWEEP_OFFSET_KEY;
31 
32 import com.google.uwb.support.base.Params;
33 import com.google.uwb.support.base.ProtocolVersion;
34 import com.google.uwb.support.radar.RadarOpenSessionParams;
35 
36 import java.nio.ByteBuffer;
37 
38 /** Radar encoder */
39 public class RadarEncoder extends TlvEncoder {
40     @Override
getTlvBuffer(Params param, ProtocolVersion protocolVersion)41     public TlvBuffer getTlvBuffer(Params param, ProtocolVersion protocolVersion) {
42         if (param instanceof RadarOpenSessionParams) {
43             return getTlvBufferFromRadarOpenSessionParams(param);
44         }
45         return null;
46     }
47 
getTlvBufferFromRadarOpenSessionParams(Params baseParam)48     private TlvBuffer getTlvBufferFromRadarOpenSessionParams(Params baseParam) {
49         RadarOpenSessionParams params = (RadarOpenSessionParams) baseParam;
50 
51         TlvBuffer.Builder tlvBufferBuilder =
52                 new TlvBuffer.Builder()
53                         .putByteArray(
54                                 RADAR_TIMING_PARAMS_KEY,
55                                 getRadarTimingParams(params)) // RADAR_TIMING_PARAMS
56                         .putByte(
57                                 SAMPLES_PER_SWEEP_KEY,
58                                 (byte) params.getSamplesPerSweep()) // SAMPLES_PER_SWEEP
59                         .putByte(
60                                 CHANNEL_NUMBER_KEY,
61                                 (byte) params.getChannelNumber()) // CHANNEL_NUMBER
62                         .putShort(SWEEP_OFFSET_KEY, (short) params.getSweepOffset()) // SWEEP_OFFSET
63                         .putByte(
64                                 RFRAME_CONFIG_KEY, (byte) params.getRframeConfig()) // RFRAME_CONFIG
65                         .putByte(
66                                 PREAMBLE_DURATION_KEY,
67                                 (byte) params.getPreambleDuration()) // PREAMBLE_DURATION
68                         .putByte(
69                                 PREAMBLE_CODE_INDEX_KEY,
70                                 (byte) params.getPreambleCodeIndex()) // PREAMBLE_CODE_INDEX
71                         .putByte(
72                                 SESSION_PRIORITY_KEY,
73                                 (byte) params.getSessionPriority()) // SESSION_PRIORITY
74                         .putByte(
75                                 BITS_PER_SAMPLE_KEY,
76                                 (byte) params.getBitsPerSample()) // BITS_PER_SAMPLE
77                         .putByte(PRF_MODE_KEY, (byte) params.getPrfMode()) // PRF_MODE
78                         .putShort(
79                                 NUMBER_OF_BURSTS_KEY,
80                                 (short) params.getNumberOfBursts()) // NUMBER_OF_BURSTS
81                         .putByte(
82                                 RADAR_DATA_TYPE_KEY,
83                                 (byte) params.getRadarDataType()); // RADAR_DATA_TYPE
84         return tlvBufferBuilder.build();
85     }
86 
getRadarTimingParams(RadarOpenSessionParams params)87     private byte[] getRadarTimingParams(RadarOpenSessionParams params) {
88         ByteBuffer buffer = ByteBuffer.allocate(7);
89         buffer.put(TlvUtil.getLeBytes(params.getBurstPeriod()));
90         buffer.put(TlvUtil.getLeBytes((short) params.getSweepPeriod()));
91         buffer.put((byte) params.getSweepsPerBurst());
92         return buffer.array();
93     }
94 }
95