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. 31 * This class represents a Transport Block 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"; 38 private final int mOrgId; 39 private final int mTdsFlags; 40 private final int mTransportDataLength; 41 private final byte[] mTransportData; 42 43 /** 44 * Creates an instance of TransportBlock from raw data. 45 * 46 * @param orgId the Organization ID 47 * @param tdsFlags the TDS flags 48 * @param transportDataLength the total length of the Transport Data 49 * @param transportData the Transport Data 50 */ TransportBlock(int orgId, int tdsFlags, int transportDataLength, @Nullable byte[] transportData)51 public TransportBlock(int orgId, int tdsFlags, int transportDataLength, 52 @Nullable byte[] transportData) { 53 mOrgId = orgId; 54 mTdsFlags = tdsFlags; 55 mTransportDataLength = transportDataLength; 56 mTransportData = transportData; 57 } 58 TransportBlock(Parcel in)59 private TransportBlock(Parcel in) { 60 mOrgId = in.readInt(); 61 mTdsFlags = in.readInt(); 62 mTransportDataLength = in.readInt(); 63 if (mTransportDataLength > 0) { 64 mTransportData = new byte[mTransportDataLength]; 65 in.readByteArray(mTransportData); 66 } else { 67 mTransportData = null; 68 } 69 } 70 71 @Override writeToParcel(@onNull Parcel dest, int flags)72 public void writeToParcel(@NonNull Parcel dest, int flags) { 73 dest.writeInt(mOrgId); 74 dest.writeInt(mTdsFlags); 75 dest.writeInt(mTransportDataLength); 76 if (mTransportData != null) { 77 dest.writeByteArray(mTransportData); 78 } 79 } 80 81 /** 82 * @hide 83 */ 84 @Override describeContents()85 public int describeContents() { 86 return 0; 87 } 88 89 /** 90 * @hide 91 */ 92 @Override equals(@ullable Object obj)93 public boolean equals(@Nullable Object obj) { 94 if (this == obj) { 95 return true; 96 } 97 if (obj == null || getClass() != obj.getClass()) { 98 return false; 99 } 100 TransportBlock other = (TransportBlock) obj; 101 return Arrays.equals(toByteArray(), other.toByteArray()); 102 } 103 104 public static final @NonNull Creator<TransportBlock> CREATOR = new Creator<TransportBlock>() { 105 @Override 106 public TransportBlock createFromParcel(Parcel in) { 107 return new TransportBlock(in); 108 } 109 110 @Override 111 public TransportBlock[] newArray(int size) { 112 return new TransportBlock[size]; 113 } 114 }; 115 116 /** 117 * Gets the Organization ID of the Transport Block which corresponds to one of the 118 * the Bluetooth SIG Assigned Numbers. 119 */ getOrgId()120 public int getOrgId() { 121 return mOrgId; 122 } 123 124 /** 125 * Gets the TDS flags of the Transport Block which represents the role of the device and 126 * information about its state and supported features. 127 */ getTdsFlags()128 public int getTdsFlags() { 129 return mTdsFlags; 130 } 131 132 /** 133 * Gets the total number of octets in the Transport Data field in this Transport Block. 134 */ getTransportDataLength()135 public int getTransportDataLength() { 136 return mTransportDataLength; 137 } 138 139 /** 140 * Gets the Transport Data of the Transport Block which contains organization-specific data. 141 */ 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