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 switch (device.getInternalType()) { 49 case AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP: 50 String address = device.getAddress().replace(":", ""); 51 if (address.length() != 12) { 52 return null; 53 } 54 address = "0x" + address; 55 long lsb = LSB_PREFIX_BT; 56 try { 57 lsb |= Long.decode(address).longValue(); 58 } catch (NumberFormatException e) { 59 return null; 60 } 61 if (AudioService.DEBUG_DEVICES) { 62 Slog.i(TAG, "uuidFromAudioDeviceAttributes lsb: " + Long.toHexString(lsb)); 63 } 64 return new UUID(0, lsb); 65 default: 66 // Handle other device types here 67 return null; 68 } 69 } 70 } 71