1 /* 2 * Copyright (C) 2014 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.bluetooth.le; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 import android.util.Log; 24 25 import java.nio.BufferOverflowException; 26 import java.nio.ByteBuffer; 27 import java.util.Arrays; 28 29 /** 30 * Wrapper for Transport Discovery Data Transport Blocks. This class represents a Transport Block 31 * from a Transport Discovery Data. 32 * 33 * @see TransportDiscoveryData 34 * @see AdvertiseData 35 */ 36 public final class TransportBlock implements Parcelable { 37 private static final String TAG = TransportBlock.class.getSimpleName(); 38 39 private final int mOrgId; 40 private final int mTdsFlags; 41 private final int mTransportDataLength; 42 private final byte[] mTransportData; 43 44 /** 45 * Creates an instance of TransportBlock from raw data. 46 * 47 * @param orgId the Organization ID 48 * @param tdsFlags the TDS flags 49 * @param transportDataLength the total length of the Transport Data 50 * @param transportData the Transport Data 51 */ TransportBlock( int orgId, int tdsFlags, int transportDataLength, @Nullable byte[] transportData)52 public TransportBlock( 53 int orgId, int tdsFlags, int transportDataLength, @Nullable byte[] transportData) { 54 mOrgId = orgId; 55 mTdsFlags = tdsFlags; 56 mTransportDataLength = transportDataLength; 57 mTransportData = transportData; 58 } 59 TransportBlock(Parcel in)60 private TransportBlock(Parcel in) { 61 mOrgId = in.readInt(); 62 mTdsFlags = in.readInt(); 63 mTransportDataLength = in.readInt(); 64 if (mTransportDataLength > 0) { 65 mTransportData = new byte[mTransportDataLength]; 66 in.readByteArray(mTransportData); 67 } else { 68 mTransportData = null; 69 } 70 } 71 72 @Override writeToParcel(@onNull Parcel dest, int flags)73 public void writeToParcel(@NonNull Parcel dest, int flags) { 74 dest.writeInt(mOrgId); 75 dest.writeInt(mTdsFlags); 76 dest.writeInt(mTransportDataLength); 77 if (mTransportData != null) { 78 dest.writeByteArray(mTransportData); 79 } 80 } 81 82 /** @hide */ 83 @Override describeContents()84 public int describeContents() { 85 return 0; 86 } 87 88 /** @hide */ 89 @Override equals(@ullable Object obj)90 public boolean equals(@Nullable Object obj) { 91 if (this == obj) { 92 return true; 93 } 94 if (obj == null || getClass() != obj.getClass()) { 95 return false; 96 } 97 TransportBlock other = (TransportBlock) obj; 98 return Arrays.equals(toByteArray(), other.toByteArray()); 99 } 100 101 /** @hide */ 102 @Override hashCode()103 public int hashCode() { 104 return Arrays.hashCode(toByteArray()); 105 } 106 107 public static final @NonNull Creator<TransportBlock> CREATOR = 108 new Creator<TransportBlock>() { 109 @Override 110 public TransportBlock createFromParcel(Parcel in) { 111 return new TransportBlock(in); 112 } 113 114 @Override 115 public TransportBlock[] newArray(int size) { 116 return new TransportBlock[size]; 117 } 118 }; 119 120 /** 121 * Gets the Organization ID of the Transport Block which corresponds to one of the Bluetooth SIG 122 * Assigned Numbers. 123 */ getOrgId()124 public int getOrgId() { 125 return mOrgId; 126 } 127 128 /** 129 * Gets the TDS flags of the Transport Block which represents the role of the device and 130 * information about its state and supported features. 131 */ getTdsFlags()132 public int getTdsFlags() { 133 return mTdsFlags; 134 } 135 136 /** Gets the total number of octets in the Transport Data field in this Transport Block. */ getTransportDataLength()137 public int getTransportDataLength() { 138 return mTransportDataLength; 139 } 140 141 /** Gets the Transport Data of the Transport Block which contains organization-specific data. */ 142 @Nullable getTransportData()143 public byte[] getTransportData() { 144 return mTransportData; 145 } 146 147 /** 148 * Converts this TransportBlock to byte array 149 * 150 * @return byte array representation of this Transport Block or null if the conversion failed 151 */ 152 @Nullable toByteArray()153 public byte[] toByteArray() { 154 try { 155 ByteBuffer buffer = ByteBuffer.allocate(totalBytes()); 156 buffer.put((byte) mOrgId); 157 buffer.put((byte) mTdsFlags); 158 buffer.put((byte) mTransportDataLength); 159 if (mTransportData != null) { 160 buffer.put(mTransportData); 161 } 162 return buffer.array(); 163 } catch (BufferOverflowException e) { 164 Log.e(TAG, "Error converting to byte array: " + e.toString()); 165 return null; 166 } 167 } 168 169 /** 170 * @return total byte count of this TransportBlock 171 */ totalBytes()172 public int totalBytes() { 173 // 3 uint8 + byte[] length 174 int size = 3 + mTransportDataLength; 175 return size; 176 } 177 } 178