1 /* 2 * Copyright (C) 2021 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 com.android.server.uwb.config.ConfigParam; 20 import com.android.server.uwb.data.UwbCccConstants; 21 import com.android.server.uwb.data.UwbUciConstants; 22 23 import com.google.uwb.support.base.Params; 24 import com.google.uwb.support.ccc.CccOpenRangingParams; 25 import com.google.uwb.support.ccc.CccParams; 26 import com.google.uwb.support.fira.FiraParams; 27 28 public class CccEncoder extends TlvEncoder { 29 @Override getTlvBuffer(Params param)30 public TlvBuffer getTlvBuffer(Params param) { 31 if (param instanceof CccOpenRangingParams) { 32 return getTlvBufferFromCccOpenRangingParams(param); 33 } 34 return null; 35 } 36 getTlvBufferFromCccOpenRangingParams(Params baseParam)37 private TlvBuffer getTlvBufferFromCccOpenRangingParams(Params baseParam) { 38 CccOpenRangingParams params = (CccOpenRangingParams) baseParam; 39 int hoppingConfig = params.getHoppingConfigMode(); 40 int hoppingSequence = params.getHoppingSequence(); 41 42 int hoppingMode = CccParams.HOPPING_CONFIG_MODE_NONE; 43 byte[] protocolVer = params.getProtocolVersion().toBytes(); 44 45 switch (hoppingConfig) { 46 47 case CccParams.HOPPING_CONFIG_MODE_CONTINUOUS: 48 if (hoppingSequence == CccParams.HOPPING_SEQUENCE_DEFAULT) { 49 hoppingMode = UwbCccConstants.HOPPING_CONFIG_MODE_CONTINUOUS_DEFAULT; 50 } else { 51 hoppingMode = UwbCccConstants.HOPPING_CONFIG_MODE_CONTINUOUS_AES; 52 } 53 break; 54 case CccParams.HOPPING_CONFIG_MODE_ADAPTIVE: 55 if (hoppingSequence == CccParams.HOPPING_SEQUENCE_DEFAULT) { 56 hoppingMode = UwbCccConstants.HOPPING_CONFIG_MODE_MODE_ADAPTIVE_DEFAULT; 57 } else { 58 hoppingMode = UwbCccConstants.HOPPING_CONFIG_MODE_MODE_ADAPTIVE_AES; 59 } 60 break; 61 } 62 63 TlvBuffer.Builder tlvBufferBuilder = new TlvBuffer.Builder() 64 .putByte(ConfigParam.DEVICE_TYPE, 65 (byte) UwbUciConstants.DEVICE_TYPE_CONTROLLER) // DEVICE_TYPE 66 .putByte(ConfigParam.STS_CONFIG, 67 (byte) UwbUciConstants.STS_MODE_DYNAMIC) // STS_CONFIG 68 .putByte(ConfigParam.CHANNEL_NUMBER, (byte) params.getChannel()) // CHANNEL_ID 69 .putByte(ConfigParam.NUMBER_OF_CONTROLEES, 70 (byte) params.getNumResponderNodes()) // NUMBER_OF_ANCHORS 71 .putInt(ConfigParam.RANGING_INTERVAL, 72 params.getRanMultiplier() * 96) //RANGING_INTERVAL = RAN_Multiplier * 96 73 .putByte(ConfigParam.RANGE_DATA_NTF_CONFIG, 74 (byte) UwbUciConstants.RANGE_DATA_NTF_CONFIG_DISABLE) // RNG_DATA_NTF 75 .putByte(ConfigParam.DEVICE_ROLE, 76 (byte) UwbUciConstants.RANGING_DEVICE_ROLE_INITIATOR) // DEVICE_ROLE 77 .putByte(ConfigParam.MULTI_NODE_MODE, 78 (byte) FiraParams.MULTI_NODE_MODE_ONE_TO_MANY) // MULTI_NODE_MODE 79 .putByte(ConfigParam.SLOTS_PER_RR, 80 (byte) params.getNumSlotsPerRound()) // SLOTS_PER_RR 81 .putByte(ConfigParam.HOPPING_MODE, (byte) hoppingMode) // HOPPING_MODE 82 .putByteArray(ConfigParam.RANGING_PROTOCOL_VER, 83 ConfigParam.RANGING_PROTOCOL_VER_BYTE_COUNT, 84 new byte[] { protocolVer[1], protocolVer[0] }) // RANGING_PROTOCOL_VER 85 .putShort(ConfigParam.UWB_CONFIG_ID, (short) params.getUwbConfig()) // UWB_CONFIG_ID 86 .putByte(ConfigParam.PULSESHAPE_COMBO, 87 params.getPulseShapeCombo().toBytes()[0]) // PULSESHAPE_COMBO 88 .putShort(ConfigParam.URSK_TTL, (short) 0x2D0) // URSK_TTL 89 // T(Slotk) = N(Chap_per_Slot) * T(Chap) 90 // T(Chap) = 400RSTU 91 // reference : digital key release 3 20.2 MAC Time Grid 92 .putShort(ConfigParam.SLOT_DURATION, 93 (short) (params.getNumChapsPerSlot() * 400)) // SLOT_DURATION 94 .putByte(ConfigParam.PREAMBLE_CODE_INDEX, 95 (byte) params.getSyncCodeIndex()); // PREAMBLE_CODE_INDEX 96 if (params.getLastStsIndexUsed() != CccParams.LAST_STS_INDEX_USED_UNSET) { 97 tlvBufferBuilder.putInt( 98 ConfigParam.LAST_STS_INDEX_USED, params.getLastStsIndexUsed()); 99 } 100 if (params.getInitiationTimeMs() != CccParams.UWB_INITIATION_TIME_MS_UNSET) { 101 tlvBufferBuilder.putLong( 102 ConfigParam.UWB_INITIATION_TIME, params.getInitiationTimeMs()); 103 } 104 return tlvBufferBuilder.build(); 105 } 106 } 107