1 /*
2  * Copyright (C) 2022 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.audio;
18 
19 import android.media.AudioDeviceAttributes;
20 import android.media.AudioSystem;
21 import android.util.Slog;
22 
23 import java.util.UUID;
24 
25 /**
26  *  UuidUtils class implements helper functions to handle unique identifiers
27  *  used to associate head tracking sensors to audio devices.
28  */
29 class UuidUtils {
30     private static final String TAG = "AudioService.UuidUtils";
31 
32     private static final long LSB_PREFIX_MASK = 0xFFFF000000000000L;
33     private static final long LSB_SUFFIX_MASK = 0x0000FFFFFFFFFFFFL;
34     // The sensor UUID for Bluetooth devices is defined as follows:
35     // - 8 most significant bytes: All 0s
36     // - 8 most significant bytes: Ascii B, Ascii T, Device MAC address on 6 bytes
37     private static final long LSB_PREFIX_BT = 0x4254000000000000L;
38 
39     /**
40      * Special UUID for a head tracking sensor not associated with an audio device.
41      */
42     public static final UUID STANDALONE_UUID = new UUID(0, 0);
43 
44     /**
45      *  Generate a headtracking UUID from AudioDeviceAttributes
46      */
uuidFromAudioDeviceAttributes(AudioDeviceAttributes device)47     public static UUID uuidFromAudioDeviceAttributes(AudioDeviceAttributes device) {
48         if (!AudioSystem.isBluetoothA2dpOutDevice(device.getInternalType())
49                 && !AudioSystem.isBluetoothLeOutDevice(device.getInternalType())) {
50             return null;
51         }
52         String address = device.getAddress().replace(":", "");
53         if (address.length() != 12) {
54             return null;
55         }
56         address = "0x" + address;
57         long lsb = LSB_PREFIX_BT;
58         try {
59             lsb |= Long.decode(address).longValue();
60         } catch (NumberFormatException e) {
61             return null;
62         }
63         if (AudioService.DEBUG_DEVICES) {
64             Slog.i(TAG, "uuidFromAudioDeviceAttributes lsb: " + Long.toHexString(lsb));
65         }
66         return new UUID(0, lsb);
67     }
68 }
69