1 /* 2 * Copyright 2020 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 android.uwb; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.SystemApi; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 25 import java.util.Arrays; 26 27 /** 28 * A class representing a UWB address 29 * 30 * @hide 31 */ 32 @SystemApi 33 public final class UwbAddress implements Parcelable { 34 public static final int SHORT_ADDRESS_BYTE_LENGTH = 2; 35 public static final int EXTENDED_ADDRESS_BYTE_LENGTH = 8; 36 37 private final byte[] mAddressBytes; 38 UwbAddress(byte[] address)39 private UwbAddress(byte[] address) { 40 mAddressBytes = address; 41 } 42 43 /** 44 * Create a {@link UwbAddress} from a byte array. 45 * 46 * <p>If the provided array is {@link #SHORT_ADDRESS_BYTE_LENGTH} bytes, a short address is 47 * created. If the provided array is {@link #EXTENDED_ADDRESS_BYTE_LENGTH} bytes, then an 48 * extended address is created. 49 * 50 * @param address a byte array to convert to a {@link UwbAddress} 51 * @return a {@link UwbAddress} created from the input byte array 52 * @throws IllegalArgumentException when the length is not one of 53 * {@link #SHORT_ADDRESS_BYTE_LENGTH} or {@link #EXTENDED_ADDRESS_BYTE_LENGTH} bytes 54 */ 55 @NonNull fromBytes(@onNull byte[] address)56 public static UwbAddress fromBytes(@NonNull byte[] address) { 57 if (address.length != SHORT_ADDRESS_BYTE_LENGTH 58 && address.length != EXTENDED_ADDRESS_BYTE_LENGTH) { 59 throw new IllegalArgumentException("Invalid UwbAddress length " + address.length); 60 } 61 return new UwbAddress(address); 62 } 63 64 /** 65 * Get the address as a byte array 66 * 67 * @return the byte representation of this {@link UwbAddress} 68 */ 69 @NonNull toBytes()70 public byte[] toBytes() { 71 return mAddressBytes; 72 } 73 74 /** 75 * The length of the address in bytes 76 * <p>Possible values are {@link #SHORT_ADDRESS_BYTE_LENGTH} and 77 * {@link #EXTENDED_ADDRESS_BYTE_LENGTH}. 78 */ size()79 public int size() { 80 return mAddressBytes.length; 81 } 82 83 @NonNull 84 @Override toString()85 public String toString() { 86 StringBuilder builder = new StringBuilder("0x"); 87 for (byte addressByte : mAddressBytes) { 88 builder.append(String.format("%02X", addressByte)); 89 } 90 return builder.toString(); 91 } 92 93 @Override equals(@ullable Object obj)94 public boolean equals(@Nullable Object obj) { 95 if (obj instanceof UwbAddress) { 96 return Arrays.equals(mAddressBytes, ((UwbAddress) obj).toBytes()); 97 } 98 return false; 99 } 100 101 @Override hashCode()102 public int hashCode() { 103 return Arrays.hashCode(mAddressBytes); 104 } 105 106 @Override describeContents()107 public int describeContents() { 108 return 0; 109 } 110 111 @Override writeToParcel(@onNull Parcel dest, int flags)112 public void writeToParcel(@NonNull Parcel dest, int flags) { 113 dest.writeInt(mAddressBytes.length); 114 dest.writeByteArray(mAddressBytes); 115 } 116 117 public static final @android.annotation.NonNull Creator<UwbAddress> CREATOR = 118 new Creator<UwbAddress>() { 119 @Override 120 public UwbAddress createFromParcel(Parcel in) { 121 byte[] address = new byte[in.readInt()]; 122 in.readByteArray(address); 123 return UwbAddress.fromBytes(address); 124 } 125 126 @Override 127 public UwbAddress[] newArray(int size) { 128 return new UwbAddress[size]; 129 } 130 }; 131 } 132