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 java.nio.ByteBuffer; 20 import java.nio.ByteOrder; 21 22 public class TlvUtil { getBytes(byte data)23 public static final byte[] getBytes(byte data) { 24 ByteBuffer buffer = ByteBuffer.allocate(Byte.BYTES).put(data); 25 return buffer.array(); 26 } 27 getBytes(short data)28 public static final byte[] getBytes(short data) { 29 ByteBuffer buffer = ByteBuffer.allocate(Short.BYTES).order( 30 ByteOrder.BIG_ENDIAN).putShort(data); 31 return buffer.array(); 32 } 33 getLeBytes(short data)34 public static final byte[] getLeBytes(short data) { 35 ByteBuffer buffer = ByteBuffer.allocate(Short.BYTES).order( 36 ByteOrder.LITTLE_ENDIAN).putShort(data); 37 return buffer.array(); 38 } 39 getBytes(int data)40 public static final byte[] getBytes(int data) { 41 ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES).order( 42 ByteOrder.BIG_ENDIAN).putInt(data); 43 return buffer.array(); 44 } 45 getLeBytes(int data)46 public static final byte[] getLeBytes(int data) { 47 ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES).order( 48 ByteOrder.LITTLE_ENDIAN).putInt(data); 49 return buffer.array(); 50 } 51 getBytes(long data)52 public static final byte[] getBytes(long data) { 53 ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES).order( 54 ByteOrder.BIG_ENDIAN).putLong(data); 55 return buffer.array(); 56 } 57 getLeBytes(long data)58 public static final byte[] getLeBytes(long data) { 59 ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES).order( 60 ByteOrder.LITTLE_ENDIAN).putLong(data); 61 return buffer.array(); 62 } 63 getReverseBytes(byte[] data)64 public static final byte[] getReverseBytes(byte[] data) { 65 byte[] buffer = new byte[data.length]; 66 for (int i = 0; i < data.length; i++) { 67 buffer[i] = data[data.length - 1 - i]; 68 } 69 return buffer; 70 } 71 getBytes(int data, int start, int length)72 public static final byte[] getBytes(int data, int start, int length) { 73 ByteBuffer srcBuf = ByteBuffer.allocate(Integer.BYTES).putInt(data); 74 ByteBuffer dstBuf = ByteBuffer.allocate(length); 75 srcBuf.position(start); 76 dstBuf.put(srcBuf); 77 return dstBuf.array(); 78 } 79 getBytesWithLeftPadding(int size, byte[] data)80 public static final byte[] getBytesWithLeftPadding(int size, byte[] data) { 81 ByteBuffer buffer = ByteBuffer.allocate(size); 82 int startOffset = size - data.length; 83 buffer.position(startOffset); 84 buffer.put(data); 85 return buffer.array(); 86 } 87 getBytesWithRightPadding(int size, byte[] data)88 public static final byte[] getBytesWithRightPadding(int size, byte[] data) { 89 ByteBuffer buffer = ByteBuffer.allocate(size).put(data); 90 return buffer.array(); 91 } 92 93 /** Convert RSTU to microsecond */ rstuToUs(int value)94 public static final int rstuToUs(int value) { 95 return (int) (value * 416 / 499.2); 96 } 97 } 98