1/*
2 * Copyright (C) 2009 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
17package android.bluetooth;
18
19import android.os.ParcelUuid;
20
21import java.nio.ByteBuffer;
22import java.nio.ByteOrder;
23import java.util.Arrays;
24import java.util.HashSet;
25import java.util.UUID;
26
27/**
28 * Static helper methods and constants to decode the ParcelUuid of remote devices.
29 *
30 * @hide
31 */
32public final class BluetoothUuid {
33
34    /* See Bluetooth Assigned Numbers document - SDP section, to get the values of UUIDs
35     * for the various services.
36     *
37     * The following 128 bit values are calculated as:
38     *  uuid * 2^96 + BASE_UUID
39     */
40    public static final ParcelUuid AudioSink =
41            ParcelUuid.fromString("0000110B-0000-1000-8000-00805F9B34FB");
42    public static final ParcelUuid AudioSource =
43            ParcelUuid.fromString("0000110A-0000-1000-8000-00805F9B34FB");
44    public static final ParcelUuid AdvAudioDist =
45            ParcelUuid.fromString("0000110D-0000-1000-8000-00805F9B34FB");
46    public static final ParcelUuid HSP =
47            ParcelUuid.fromString("00001108-0000-1000-8000-00805F9B34FB");
48    public static final ParcelUuid HSP_AG =
49            ParcelUuid.fromString("00001112-0000-1000-8000-00805F9B34FB");
50    public static final ParcelUuid Handsfree =
51            ParcelUuid.fromString("0000111E-0000-1000-8000-00805F9B34FB");
52    public static final ParcelUuid Handsfree_AG =
53            ParcelUuid.fromString("0000111F-0000-1000-8000-00805F9B34FB");
54    public static final ParcelUuid AvrcpController =
55            ParcelUuid.fromString("0000110E-0000-1000-8000-00805F9B34FB");
56    public static final ParcelUuid AvrcpTarget =
57            ParcelUuid.fromString("0000110C-0000-1000-8000-00805F9B34FB");
58    public static final ParcelUuid ObexObjectPush =
59            ParcelUuid.fromString("00001105-0000-1000-8000-00805f9b34fb");
60    public static final ParcelUuid Hid =
61            ParcelUuid.fromString("00001124-0000-1000-8000-00805f9b34fb");
62    public static final ParcelUuid Hogp =
63            ParcelUuid.fromString("00001812-0000-1000-8000-00805f9b34fb");
64    public static final ParcelUuid PANU =
65            ParcelUuid.fromString("00001115-0000-1000-8000-00805F9B34FB");
66    public static final ParcelUuid NAP =
67            ParcelUuid.fromString("00001116-0000-1000-8000-00805F9B34FB");
68    public static final ParcelUuid BNEP =
69            ParcelUuid.fromString("0000000f-0000-1000-8000-00805F9B34FB");
70    public static final ParcelUuid PBAP_PCE =
71            ParcelUuid.fromString("0000112e-0000-1000-8000-00805F9B34FB");
72    public static final ParcelUuid PBAP_PSE =
73            ParcelUuid.fromString("0000112f-0000-1000-8000-00805F9B34FB");
74    public static final ParcelUuid MAP =
75            ParcelUuid.fromString("00001134-0000-1000-8000-00805F9B34FB");
76    public static final ParcelUuid MNS =
77            ParcelUuid.fromString("00001133-0000-1000-8000-00805F9B34FB");
78    public static final ParcelUuid MAS =
79            ParcelUuid.fromString("00001132-0000-1000-8000-00805F9B34FB");
80    public static final ParcelUuid SAP =
81            ParcelUuid.fromString("0000112D-0000-1000-8000-00805F9B34FB");
82    public static final ParcelUuid HearingAid =
83            ParcelUuid.fromString("0000FDF0-0000-1000-8000-00805f9b34fb");
84
85    public static final ParcelUuid BASE_UUID =
86            ParcelUuid.fromString("00000000-0000-1000-8000-00805F9B34FB");
87
88    /** Length of bytes for 16 bit UUID */
89    public static final int UUID_BYTES_16_BIT = 2;
90    /** Length of bytes for 32 bit UUID */
91    public static final int UUID_BYTES_32_BIT = 4;
92    /** Length of bytes for 128 bit UUID */
93    public static final int UUID_BYTES_128_BIT = 16;
94
95    public static final ParcelUuid[] RESERVED_UUIDS = {
96            AudioSink, AudioSource, AdvAudioDist, HSP, Handsfree, AvrcpController, AvrcpTarget,
97            ObexObjectPush, PANU, NAP, MAP, MNS, MAS, SAP};
98
99    public static boolean isAudioSource(ParcelUuid uuid) {
100        return uuid.equals(AudioSource);
101    }
102
103    public static boolean isAudioSink(ParcelUuid uuid) {
104        return uuid.equals(AudioSink);
105    }
106
107    public static boolean isAdvAudioDist(ParcelUuid uuid) {
108        return uuid.equals(AdvAudioDist);
109    }
110
111    public static boolean isHandsfree(ParcelUuid uuid) {
112        return uuid.equals(Handsfree);
113    }
114
115    public static boolean isHeadset(ParcelUuid uuid) {
116        return uuid.equals(HSP);
117    }
118
119    public static boolean isAvrcpController(ParcelUuid uuid) {
120        return uuid.equals(AvrcpController);
121    }
122
123    public static boolean isAvrcpTarget(ParcelUuid uuid) {
124        return uuid.equals(AvrcpTarget);
125    }
126
127    public static boolean isInputDevice(ParcelUuid uuid) {
128        return uuid.equals(Hid);
129    }
130
131    public static boolean isPanu(ParcelUuid uuid) {
132        return uuid.equals(PANU);
133    }
134
135    public static boolean isNap(ParcelUuid uuid) {
136        return uuid.equals(NAP);
137    }
138
139    public static boolean isBnep(ParcelUuid uuid) {
140        return uuid.equals(BNEP);
141    }
142
143    public static boolean isMap(ParcelUuid uuid) {
144        return uuid.equals(MAP);
145    }
146
147    public static boolean isMns(ParcelUuid uuid) {
148        return uuid.equals(MNS);
149    }
150
151    public static boolean isMas(ParcelUuid uuid) {
152        return uuid.equals(MAS);
153    }
154
155    public static boolean isSap(ParcelUuid uuid) {
156        return uuid.equals(SAP);
157    }
158
159    /**
160     * Returns true if ParcelUuid is present in uuidArray
161     *
162     * @param uuidArray - Array of ParcelUuids
163     * @param uuid
164     */
165    public static boolean isUuidPresent(ParcelUuid[] uuidArray, ParcelUuid uuid) {
166        if ((uuidArray == null || uuidArray.length == 0) && uuid == null) {
167            return true;
168        }
169
170        if (uuidArray == null) {
171            return false;
172        }
173
174        for (ParcelUuid element : uuidArray) {
175            if (element.equals(uuid)) return true;
176        }
177        return false;
178    }
179
180    /**
181     * Returns true if there any common ParcelUuids in uuidA and uuidB.
182     *
183     * @param uuidA - List of ParcelUuids
184     * @param uuidB - List of ParcelUuids
185     */
186    public static boolean containsAnyUuid(ParcelUuid[] uuidA, ParcelUuid[] uuidB) {
187        if (uuidA == null && uuidB == null) return true;
188
189        if (uuidA == null) {
190            return uuidB.length == 0;
191        }
192
193        if (uuidB == null) {
194            return uuidA.length == 0;
195        }
196
197        HashSet<ParcelUuid> uuidSet = new HashSet<ParcelUuid>(Arrays.asList(uuidA));
198        for (ParcelUuid uuid : uuidB) {
199            if (uuidSet.contains(uuid)) return true;
200        }
201        return false;
202    }
203
204    /**
205     * Returns true if all the ParcelUuids in ParcelUuidB are present in
206     * ParcelUuidA
207     *
208     * @param uuidA - Array of ParcelUuidsA
209     * @param uuidB - Array of ParcelUuidsB
210     */
211    public static boolean containsAllUuids(ParcelUuid[] uuidA, ParcelUuid[] uuidB) {
212        if (uuidA == null && uuidB == null) return true;
213
214        if (uuidA == null) {
215            return uuidB.length == 0;
216        }
217
218        if (uuidB == null) return true;
219
220        HashSet<ParcelUuid> uuidSet = new HashSet<ParcelUuid>(Arrays.asList(uuidA));
221        for (ParcelUuid uuid : uuidB) {
222            if (!uuidSet.contains(uuid)) return false;
223        }
224        return true;
225    }
226
227    /**
228     * Extract the Service Identifier or the actual uuid from the Parcel Uuid.
229     * For example, if 0000110B-0000-1000-8000-00805F9B34FB is the parcel Uuid,
230     * this function will return 110B
231     *
232     * @param parcelUuid
233     * @return the service identifier.
234     */
235    public static int getServiceIdentifierFromParcelUuid(ParcelUuid parcelUuid) {
236        UUID uuid = parcelUuid.getUuid();
237        long value = (uuid.getMostSignificantBits() & 0xFFFFFFFF00000000L) >>> 32;
238        return (int) value;
239    }
240
241    /**
242     * Parse UUID from bytes. The {@code uuidBytes} can represent a 16-bit, 32-bit or 128-bit UUID,
243     * but the returned UUID is always in 128-bit format.
244     * Note UUID is little endian in Bluetooth.
245     *
246     * @param uuidBytes Byte representation of uuid.
247     * @return {@link ParcelUuid} parsed from bytes.
248     * @throws IllegalArgumentException If the {@code uuidBytes} cannot be parsed.
249     */
250    public static ParcelUuid parseUuidFrom(byte[] uuidBytes) {
251        if (uuidBytes == null) {
252            throw new IllegalArgumentException("uuidBytes cannot be null");
253        }
254        int length = uuidBytes.length;
255        if (length != UUID_BYTES_16_BIT && length != UUID_BYTES_32_BIT
256                && length != UUID_BYTES_128_BIT) {
257            throw new IllegalArgumentException("uuidBytes length invalid - " + length);
258        }
259
260        // Construct a 128 bit UUID.
261        if (length == UUID_BYTES_128_BIT) {
262            ByteBuffer buf = ByteBuffer.wrap(uuidBytes).order(ByteOrder.LITTLE_ENDIAN);
263            long msb = buf.getLong(8);
264            long lsb = buf.getLong(0);
265            return new ParcelUuid(new UUID(msb, lsb));
266        }
267
268        // For 16 bit and 32 bit UUID we need to convert them to 128 bit value.
269        // 128_bit_value = uuid * 2^96 + BASE_UUID
270        long shortUuid;
271        if (length == UUID_BYTES_16_BIT) {
272            shortUuid = uuidBytes[0] & 0xFF;
273            shortUuid += (uuidBytes[1] & 0xFF) << 8;
274        } else {
275            shortUuid = uuidBytes[0] & 0xFF;
276            shortUuid += (uuidBytes[1] & 0xFF) << 8;
277            shortUuid += (uuidBytes[2] & 0xFF) << 16;
278            shortUuid += (uuidBytes[3] & 0xFF) << 24;
279        }
280        long msb = BASE_UUID.getUuid().getMostSignificantBits() + (shortUuid << 32);
281        long lsb = BASE_UUID.getUuid().getLeastSignificantBits();
282        return new ParcelUuid(new UUID(msb, lsb));
283    }
284
285    /**
286     * Parse UUID to bytes. The returned value is shortest representation, a 16-bit, 32-bit or
287     * 128-bit UUID, Note returned value is little endian (Bluetooth).
288     *
289     * @param uuid uuid to parse.
290     * @return shortest representation of {@code uuid} as bytes.
291     * @throws IllegalArgumentException If the {@code uuid} is null.
292     */
293    public static byte[] uuidToBytes(ParcelUuid uuid) {
294        if (uuid == null) {
295            throw new IllegalArgumentException("uuid cannot be null");
296        }
297
298        if (is16BitUuid(uuid)) {
299            byte[] uuidBytes = new byte[UUID_BYTES_16_BIT];
300            int uuidVal = getServiceIdentifierFromParcelUuid(uuid);
301            uuidBytes[0] = (byte) (uuidVal & 0xFF);
302            uuidBytes[1] = (byte) ((uuidVal & 0xFF00) >> 8);
303            return uuidBytes;
304        }
305
306        if (is32BitUuid(uuid)) {
307            byte[] uuidBytes = new byte[UUID_BYTES_32_BIT];
308            int uuidVal = getServiceIdentifierFromParcelUuid(uuid);
309            uuidBytes[0] = (byte) (uuidVal & 0xFF);
310            uuidBytes[1] = (byte) ((uuidVal & 0xFF00) >> 8);
311            uuidBytes[2] = (byte) ((uuidVal & 0xFF0000) >> 16);
312            uuidBytes[3] = (byte) ((uuidVal & 0xFF000000) >> 24);
313            return uuidBytes;
314        }
315
316        // Construct a 128 bit UUID.
317        long msb = uuid.getUuid().getMostSignificantBits();
318        long lsb = uuid.getUuid().getLeastSignificantBits();
319
320        byte[] uuidBytes = new byte[UUID_BYTES_128_BIT];
321        ByteBuffer buf = ByteBuffer.wrap(uuidBytes).order(ByteOrder.LITTLE_ENDIAN);
322        buf.putLong(8, msb);
323        buf.putLong(0, lsb);
324        return uuidBytes;
325    }
326
327    /**
328     * Check whether the given parcelUuid can be converted to 16 bit bluetooth uuid.
329     *
330     * @param parcelUuid
331     * @return true if the parcelUuid can be converted to 16 bit uuid, false otherwise.
332     */
333    public static boolean is16BitUuid(ParcelUuid parcelUuid) {
334        UUID uuid = parcelUuid.getUuid();
335        if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) {
336            return false;
337        }
338        return ((uuid.getMostSignificantBits() & 0xFFFF0000FFFFFFFFL) == 0x1000L);
339    }
340
341
342    /**
343     * Check whether the given parcelUuid can be converted to 32 bit bluetooth uuid.
344     *
345     * @param parcelUuid
346     * @return true if the parcelUuid can be converted to 32 bit uuid, false otherwise.
347     */
348    public static boolean is32BitUuid(ParcelUuid parcelUuid) {
349        UUID uuid = parcelUuid.getUuid();
350        if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) {
351            return false;
352        }
353        if (is16BitUuid(parcelUuid)) {
354            return false;
355        }
356        return ((uuid.getMostSignificantBits() & 0xFFFFFFFFL) == 0x1000L);
357    }
358}
359