• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.google.uwb.support.aliro.AliroParams.RANGE_DATA_NTF_CONFIG_ENABLE_AOA_EDGE_TRIG;
20 import static com.google.uwb.support.aliro.AliroParams.RANGE_DATA_NTF_CONFIG_ENABLE_AOA_LEVEL_TRIG;
21 import static com.google.uwb.support.aliro.AliroParams.RANGE_DATA_NTF_CONFIG_ENABLE_PROXIMITY_AOA_EDGE_TRIG;
22 import static com.google.uwb.support.aliro.AliroParams.RANGE_DATA_NTF_CONFIG_ENABLE_PROXIMITY_AOA_LEVEL_TRIG;
23 
24 import com.android.server.uwb.UwbInjector;
25 import com.android.server.uwb.config.ConfigParam;
26 import com.android.server.uwb.data.UwbAliroConstants;
27 import com.android.server.uwb.data.UwbUciConstants;
28 import com.android.server.uwb.util.UwbUtil;
29 
30 import com.google.uwb.support.aliro.AliroOpenRangingParams;
31 import com.google.uwb.support.aliro.AliroParams;
32 import com.google.uwb.support.base.Params;
33 import com.google.uwb.support.base.ProtocolVersion;
34 import com.google.uwb.support.fira.FiraParams;
35 
36 /**
37  * Encoder for Aliro UCI SET_APP_CONFIG CMD parameters.
38  *
39  * This started out as a copy of CccEncoder, and encodes all of the same parameters.
40  */
41 public class AliroEncoder extends TlvEncoder {
42     private final UwbInjector mUwbInjector;
43 
AliroEncoder(UwbInjector uwbInjector)44     public AliroEncoder(UwbInjector uwbInjector) {
45         mUwbInjector = uwbInjector;
46     }
47 
48     @Override
getTlvBuffer(Params param, ProtocolVersion protocolVersion)49     public TlvBuffer getTlvBuffer(Params param, ProtocolVersion protocolVersion) {
50         if (param instanceof AliroOpenRangingParams) {
51             return getTlvBufferFromAliroOpenRangingParams(param);
52         }
53         return null;
54     }
55 
getTlvBufferFromAliroOpenRangingParams(Params baseParam)56     private TlvBuffer getTlvBufferFromAliroOpenRangingParams(Params baseParam) {
57         AliroOpenRangingParams params = (AliroOpenRangingParams) baseParam;
58         int hoppingConfig = params.getHoppingConfigMode();
59         int hoppingSequence = params.getHoppingSequence();
60 
61         int hoppingMode = AliroParams.HOPPING_CONFIG_MODE_NONE;
62         byte[] protocolVer = params.getProtocolVersion().toBytes();
63 
64         switch (hoppingConfig) {
65 
66             case AliroParams.HOPPING_CONFIG_MODE_CONTINUOUS:
67                 if (hoppingSequence == AliroParams.HOPPING_SEQUENCE_DEFAULT) {
68                     hoppingMode = UwbAliroConstants.HOPPING_CONFIG_MODE_CONTINUOUS_DEFAULT;
69                 } else {
70                     hoppingMode = UwbAliroConstants.HOPPING_CONFIG_MODE_CONTINUOUS_AES;
71                 }
72                 break;
73             case AliroParams.HOPPING_CONFIG_MODE_ADAPTIVE:
74                 if (hoppingSequence == AliroParams.HOPPING_SEQUENCE_DEFAULT) {
75                     hoppingMode = UwbAliroConstants.HOPPING_CONFIG_MODE_MODE_ADAPTIVE_DEFAULT;
76                 } else {
77                     hoppingMode = UwbAliroConstants.HOPPING_CONFIG_MODE_MODE_ADAPTIVE_AES;
78                 }
79                 break;
80         }
81 
82         TlvBuffer.Builder tlvBufferBuilder = new TlvBuffer.Builder()
83                 .putByte(ConfigParam.DEVICE_TYPE,
84                         (byte) UwbUciConstants.DEVICE_TYPE_CONTROLLER) // DEVICE_TYPE
85                 .putByte(ConfigParam.STS_CONFIG,
86                         (byte) UwbUciConstants.STS_MODE_DYNAMIC) // STS_CONFIG
87                 .putByte(ConfigParam.CHANNEL_NUMBER, (byte) params.getChannel()) // CHANNEL_ID
88                 .putByte(ConfigParam.NUMBER_OF_CONTROLEES,
89                         (byte) params.getNumResponderNodes()) // NUMBER_OF_ANCHORS
90                 .putInt(ConfigParam.RANGING_INTERVAL,
91                         params.getRanMultiplier() * 96) //RANGING_INTERVAL = RAN_Multiplier * 96
92                 .putByte(ConfigParam.DEVICE_ROLE,
93                         (byte) UwbUciConstants.RANGING_DEVICE_ROLE_INITIATOR) // DEVICE_ROLE
94                 .putByte(ConfigParam.MULTI_NODE_MODE,
95                         (byte) FiraParams.MULTI_NODE_MODE_ONE_TO_MANY) // MULTI_NODE_MODE
96                 .putByte(ConfigParam.SLOTS_PER_RR,
97                         (byte) params.getNumSlotsPerRound()) // SLOTS_PER_RR
98                 .putByte(ConfigParam.HOPPING_MODE, (byte) hoppingMode) // HOPPING_MODE
99                 .putByteArray(ConfigParam.RANGING_PROTOCOL_VER,
100                         ConfigParam.RANGING_PROTOCOL_VER_BYTE_COUNT,
101                         new byte[] { protocolVer[1], protocolVer[0] }) // RANGING_PROTOCOL_VER
102                 .putShort(ConfigParam.UWB_CONFIG_ID, (short) params.getUwbConfig()) // UWB_CONFIG_ID
103                 .putByte(ConfigParam.PULSESHAPE_COMBO,
104                         params.getPulseShapeCombo().toBytes()[0]) // PULSESHAPE_COMBO
105                 .putShort(ConfigParam.URSK_TTL, (short) 0x2D0) // URSK_TTL
106                 // T(Slotk) =  N(Chap_per_Slot) * T(Chap)
107                 // T(Chap) = 400RSTU
108                 // reference : digital key release 3 20.2 MAC Time Grid
109                 .putShort(ConfigParam.SLOT_DURATION,
110                         (short) (params.getNumChapsPerSlot() * 400)) // SLOT_DURATION
111                 .putByte(ConfigParam.PREAMBLE_CODE_INDEX,
112                         (byte) params.getSyncCodeIndex()); // PREAMBLE_CODE_INDEX
113         if (params.getStsIndex() != AliroParams.STS_INDEX_UNSET) {
114             tlvBufferBuilder.putInt(ConfigParam.STS_INDEX, params.getStsIndex());
115         }
116         if (params.getAbsoluteInitiationTimeUs() > 0) {
117             tlvBufferBuilder.putLong(ConfigParam.UWB_INITIATION_TIME,
118                     params.getAbsoluteInitiationTimeUs());
119         } else if (params.getInitiationTimeMs() != AliroParams.UWB_INITIATION_TIME_MS_UNSET) {
120             tlvBufferBuilder.putLong(
121                     ConfigParam.UWB_INITIATION_TIME, params.getInitiationTimeMs());
122         }
123         // We use the same CCC DeviceConfigFacade flag for Aliro.
124         if (mUwbInjector.getDeviceConfigFacade().isCccSupportedRangeDataNtfConfig()) {
125             tlvBufferBuilder
126                     .putByte(ConfigParam.RANGE_DATA_NTF_CONFIG,
127                             (byte) params.getRangeDataNtfConfig())
128                     .putShort(ConfigParam.RANGE_DATA_NTF_PROXIMITY_NEAR,
129                             (short) params.getRangeDataNtfProximityNear())
130                     .putShort(ConfigParam.RANGE_DATA_NTF_PROXIMITY_FAR,
131                             (short) params.getRangeDataNtfProximityFar());
132 
133             if (hasAoaBoundInRangeDataNtfConfig(params.getRangeDataNtfConfig())) {
134                 tlvBufferBuilder.putShortArray(ConfigParam.RANGE_DATA_NTF_AOA_BOUND, new short[] {
135                         // TODO (b/235355249): Verify this conversion. This is using AOA value
136                         // in UwbTwoWayMeasurement to external RangingMeasurement conversion as
137                         // reference.
138                         (short) UwbUtil.twos_compliment(UwbUtil.convertFloatToQFormat(
139                                 UwbUtil.radianTodegree(
140                                         params.getRangeDataNtfAoaAzimuthLower()), 9, 7), 16),
141                         (short) UwbUtil.twos_compliment(UwbUtil.convertFloatToQFormat(
142                                 UwbUtil.radianTodegree(
143                                         params.getRangeDataNtfAoaAzimuthUpper()), 9, 7), 16),
144                         (short) UwbUtil.twos_compliment(UwbUtil.convertFloatToQFormat(
145                                 UwbUtil.radianTodegree(
146                                         params.getRangeDataNtfAoaElevationLower()), 9, 7), 16),
147                         (short) UwbUtil.twos_compliment(UwbUtil.convertFloatToQFormat(
148                                 UwbUtil.radianTodegree(
149                                         params.getRangeDataNtfAoaElevationUpper()), 9, 7), 16),
150                 });
151             }
152         } else {
153             tlvBufferBuilder
154                     .putByte(ConfigParam.RANGE_DATA_NTF_CONFIG,
155                             (byte) UwbUciConstants.RANGE_DATA_NTF_CONFIG_DISABLE); // RNG_DATA_NTF
156         }
157         return tlvBufferBuilder.build();
158     }
159 
hasAoaBoundInRangeDataNtfConfig(int rangeDataNtfConfig)160     private static boolean hasAoaBoundInRangeDataNtfConfig(int rangeDataNtfConfig) {
161         return rangeDataNtfConfig == RANGE_DATA_NTF_CONFIG_ENABLE_AOA_LEVEL_TRIG
162                 || rangeDataNtfConfig == RANGE_DATA_NTF_CONFIG_ENABLE_PROXIMITY_AOA_LEVEL_TRIG
163                 || rangeDataNtfConfig == RANGE_DATA_NTF_CONFIG_ENABLE_AOA_EDGE_TRIG
164                 || rangeDataNtfConfig == RANGE_DATA_NTF_CONFIG_ENABLE_PROXIMITY_AOA_EDGE_TRIG;
165     }
166 }
167