1 /* 2 * Copyright (C) 2024 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.bluetooth.pbapclient; 18 19 import static java.util.Objects.requireNonNull; 20 21 import android.bluetooth.BluetoothDevice; 22 import android.bluetooth.SdpPseRecord; 23 24 /** 25 * This object represents an SDP Record for the PBAP profile. It extends the framework class by 26 * housing all the supported feature masks. 27 */ 28 public class PbapSdpRecord { 29 public static final int VERSION_1_0 = 0x0100; 30 public static final int VERSION_1_1 = 0x0101; 31 public static final int VERSION_1_2 = 0x0102; 32 33 public static final int FEATURES_EXCLUDED = -1; 34 public static final int FEATURE_DOWNLOADING = 1 << 0; 35 public static final int FEATURE_BROWSING = 1 << 1; 36 public static final int FEATURE_DATABASE_IDENTIFIER = 1 << 2; 37 public static final int FEATURE_FOLDER_VERSION_COUNTERS = 1 << 3; 38 public static final int FEATURE_VCARD_SELECTING = 1 << 4; 39 public static final int FEATURE_ENHANCED_MISSED_CALLS = 1 << 5; 40 public static final int FEATURE_XBT_UCI_VCARD_PROPERTY = 1 << 6; 41 public static final int FEATURE_XBT_UID_VCARD_PROPERTY = 1 << 7; 42 public static final int FEATURE_CONTACT_REFERENCING = 1 << 8; 43 public static final int FEATURE_DEFAULT_IMAGE_FORMAT = 1 << 9; 44 45 // PBAP v1.2.3 Sec. 7.1.2 46 public static final int REPOSITORY_LOCAL_PHONEBOOK = 1 << 0; 47 public static final int REPOSITORY_SIM_CARD = 1 << 1; 48 public static final int REPOSITORY_SPEED_DIAL = 1 << 2; 49 public static final int REPOSITORY_FAVORITES = 1 << 3; 50 51 public static final int FIELD_MISSING = -1; 52 53 private final BluetoothDevice mDevice; 54 private final SdpPseRecord mSdpRecord; 55 PbapSdpRecord(BluetoothDevice device, SdpPseRecord record)56 PbapSdpRecord(BluetoothDevice device, SdpPseRecord record) { 57 mDevice = requireNonNull(device); 58 mSdpRecord = requireNonNull(record); 59 } 60 61 /** Get the device associated with this SDP record */ getDevice()62 public BluetoothDevice getDevice() { 63 return mDevice; 64 } 65 66 /** Get the profile version associated with this SDP record */ getProfileVersion()67 public int getProfileVersion() { 68 return mSdpRecord.getProfileVersion(); 69 } 70 71 /** Get the service name associated with this SDP record */ getServiceName()72 public String getServiceName() { 73 return mSdpRecord.getServiceName(); 74 } 75 76 /** Get the L2CAP PSM associated with this SDP record */ getL2capPsm()77 public int getL2capPsm() { 78 return mSdpRecord.getL2capPsm(); 79 } 80 81 /** Get the RFCOMM channel number associated with this SDP record */ getRfcommChannelNumber()82 public int getRfcommChannelNumber() { 83 return mSdpRecord.getRfcommChannelNumber(); 84 } 85 86 /** Get the supported features associated with this SDP record */ getSupportedFeatures()87 public int getSupportedFeatures() { 88 return mSdpRecord.getSupportedFeatures(); 89 } 90 91 /** Returns true if this SDP record supports a given feature */ isFeatureSupported(int feature)92 public boolean isFeatureSupported(int feature) { 93 int remoteFeatures = mSdpRecord.getSupportedFeatures(); 94 if (remoteFeatures != FIELD_MISSING) { 95 return (feature & remoteFeatures) != 0; 96 } 97 return false; 98 } 99 100 /** Git the supported repositories bitmask associated with this SDP record */ getSupportedRepositories()101 public int getSupportedRepositories() { 102 return mSdpRecord.getSupportedRepositories(); 103 } 104 105 /** Returns true if this SDP record supports a given repository */ isRepositorySupported(int repository)106 public boolean isRepositorySupported(int repository) { 107 int remoteRepositories = mSdpRecord.getSupportedRepositories(); 108 if (remoteRepositories != FIELD_MISSING) { 109 return (repository & remoteRepositories) != 0; 110 } 111 return false; 112 } 113 114 /** Get a string representation of this SDP record */ 115 @Override toString()116 public String toString() { 117 return mSdpRecord.toString(); 118 } 119 120 /** 121 * Get a string representation of any of the SDP PBAP version constants 122 * 123 * <p>Version is represented as a series of specification defined constants, in the form: 124 * 0x[Major 2 bytes][Minor 2 bytes] -> [Major].[Minor] 125 * 126 * <p>For example, 0x0102 is 1.2. 127 */ versionToString(int version)128 public static String versionToString(int version) { 129 switch (version) { 130 case FIELD_MISSING: 131 return "VERSION_UNKNOWN"; 132 case VERSION_1_0: 133 return "VERSION_1_0"; 134 case VERSION_1_1: 135 return "VERSION_1_1"; 136 case VERSION_1_2: 137 return "VERSION_1_2"; 138 default: 139 return "VERSION_UNRECOGNIZED_" + String.format("%04X", version); 140 } 141 } 142 143 /** Get a string representation of any of the SDP feature constants */ featureToString(int feature)144 public static String featureToString(int feature) { 145 switch (feature) { 146 case FEATURE_DOWNLOADING: 147 return "FEATURE_DOWNLOADING"; 148 case FEATURE_BROWSING: 149 return "FEATURE_BROWSING"; 150 case FEATURE_DATABASE_IDENTIFIER: 151 return "FEATURE_DATABASE_IDENTIFIER"; 152 case FEATURE_FOLDER_VERSION_COUNTERS: 153 return "FEATURE_FOLDER_VERSION_COUNTERS"; 154 case FEATURE_VCARD_SELECTING: 155 return "FEATURE_VCARD_SELECTING"; 156 case FEATURE_ENHANCED_MISSED_CALLS: 157 return "FEATURE_ENHANCED_MISSED_CALLS"; 158 case FEATURE_XBT_UCI_VCARD_PROPERTY: 159 return "FEATURE_XBT_UCI_VCARD_PROPERTY"; 160 case FEATURE_XBT_UID_VCARD_PROPERTY: 161 return "FEATURE_XBT_UID_VCARD_PROPERTY"; 162 case FEATURE_CONTACT_REFERENCING: 163 return "FEATURE_CONTACT_REFERENCING"; 164 case FEATURE_DEFAULT_IMAGE_FORMAT: 165 return "FEATURE_DEFAULT_IMAGE_FORMAT"; 166 default: 167 return "FEATURE_RESERVED_BIT_" + feature; 168 } 169 } 170 171 /** Get a string representation of any of the SDP repository constants */ repositoryToString(int repository)172 public static String repositoryToString(int repository) { 173 switch (repository) { 174 case REPOSITORY_LOCAL_PHONEBOOK: 175 return "REPOSITORY_LOCAL_PHONEBOOK"; 176 case REPOSITORY_SIM_CARD: 177 return "REPOSITORY_SIM_CARD"; 178 case REPOSITORY_SPEED_DIAL: 179 return "REPOSITORY_SPEED_DIAL"; 180 case REPOSITORY_FAVORITES: 181 return "REPOSITORY_FAVORITES"; 182 default: 183 return "REPOSITORY_RESERVED_BIT_" + repository; 184 } 185 } 186 } 187