1 /* 2 * Copyright 2023 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.btservice; 18 19 import android.bluetooth.OobData; 20 import android.os.ParcelUuid; 21 22 import com.android.internal.annotations.GuardedBy; 23 import com.android.internal.annotations.VisibleForTesting; 24 25 import java.io.FileDescriptor; 26 27 /** Native interface to be used by AdapterService */ 28 public class AdapterNativeInterface { 29 private static final String TAG = AdapterNativeInterface.class.getSimpleName(); 30 31 private JniCallbacks mJniCallbacks; 32 33 @GuardedBy("INSTANCE_LOCK") 34 private static AdapterNativeInterface sInstance; 35 36 private static final Object INSTANCE_LOCK = new Object(); 37 AdapterNativeInterface()38 private AdapterNativeInterface() {} 39 getInstance()40 static AdapterNativeInterface getInstance() { 41 synchronized (INSTANCE_LOCK) { 42 if (sInstance == null) { 43 sInstance = new AdapterNativeInterface(); 44 } 45 return sInstance; 46 } 47 } 48 49 /** Set singleton instance. */ 50 @VisibleForTesting setInstance(AdapterNativeInterface instance)51 public static void setInstance(AdapterNativeInterface instance) { 52 synchronized (INSTANCE_LOCK) { 53 sInstance = instance; 54 } 55 } 56 getCallbacks()57 JniCallbacks getCallbacks() { 58 return mJniCallbacks; 59 } 60 init( AdapterService service, AdapterProperties adapterProperties, boolean startRestricted, boolean isCommonCriteriaMode, int configCompareResult, String[] initFlags, boolean isAtvDevice, String userDataDirectory)61 boolean init( 62 AdapterService service, 63 AdapterProperties adapterProperties, 64 boolean startRestricted, 65 boolean isCommonCriteriaMode, 66 int configCompareResult, 67 String[] initFlags, 68 boolean isAtvDevice, 69 String userDataDirectory) { 70 mJniCallbacks = new JniCallbacks(service, adapterProperties); 71 return initNative( 72 startRestricted, 73 isCommonCriteriaMode, 74 configCompareResult, 75 initFlags, 76 isAtvDevice, 77 userDataDirectory); 78 } 79 cleanup()80 void cleanup() { 81 cleanupNative(); 82 } 83 enable()84 boolean enable() { 85 return enableNative(); 86 } 87 disable()88 boolean disable() { 89 return disableNative(); 90 } 91 setAdapterProperty(int type, byte[] val)92 boolean setAdapterProperty(int type, byte[] val) { 93 return setAdapterPropertyNative(type, val); 94 } 95 getAdapterProperties()96 boolean getAdapterProperties() { 97 return getAdapterPropertiesNative(); 98 } 99 getAdapterProperty(int type)100 boolean getAdapterProperty(int type) { 101 return getAdapterPropertyNative(type); 102 } 103 setDeviceProperty(byte[] address, int type, byte[] val)104 boolean setDeviceProperty(byte[] address, int type, byte[] val) { 105 return setDevicePropertyNative(address, type, val); 106 } 107 getDeviceProperty(byte[] address, int type)108 boolean getDeviceProperty(byte[] address, int type) { 109 return getDevicePropertyNative(address, type); 110 } 111 createBond(byte[] address, int addressType, int transport)112 boolean createBond(byte[] address, int addressType, int transport) { 113 return createBondNative(address, addressType, transport); 114 } 115 createBondOutOfBand(byte[] address, int transport, OobData p192Data, OobData p256Data)116 boolean createBondOutOfBand(byte[] address, int transport, OobData p192Data, OobData p256Data) { 117 return createBondOutOfBandNative(address, transport, p192Data, p256Data); 118 } 119 removeBond(byte[] address)120 boolean removeBond(byte[] address) { 121 return removeBondNative(address); 122 } 123 cancelBond(byte[] address)124 boolean cancelBond(byte[] address) { 125 return cancelBondNative(address); 126 } 127 pairingIsBusy()128 boolean pairingIsBusy() { 129 return pairingIsBusyNative(); 130 } 131 generateLocalOobData(int transport)132 void generateLocalOobData(int transport) { 133 generateLocalOobDataNative(transport); 134 } 135 sdpSearch(byte[] address, byte[] uuid)136 boolean sdpSearch(byte[] address, byte[] uuid) { 137 return sdpSearchNative(address, uuid); 138 } 139 getConnectionState(byte[] address)140 int getConnectionState(byte[] address) { 141 return getConnectionStateNative(address); 142 } 143 startDiscovery()144 boolean startDiscovery() { 145 return startDiscoveryNative(); 146 } 147 cancelDiscovery()148 boolean cancelDiscovery() { 149 return cancelDiscoveryNative(); 150 } 151 pinReply(byte[] address, boolean accept, int len, byte[] pin)152 boolean pinReply(byte[] address, boolean accept, int len, byte[] pin) { 153 return pinReplyNative(address, accept, len, pin); 154 } 155 sspReply(byte[] address, int type, boolean accept, int passkey)156 boolean sspReply(byte[] address, int type, boolean accept, int passkey) { 157 return sspReplyNative(address, type, accept, passkey); 158 } 159 getRemoteServices(byte[] address, int transport)160 boolean getRemoteServices(byte[] address, int transport) { 161 return getRemoteServicesNative(address, transport); 162 } 163 getRemoteMasInstances(byte[] address)164 boolean getRemoteMasInstances(byte[] address) { 165 return getRemoteMasInstancesNative(address); 166 } 167 readEnergyInfo()168 int readEnergyInfo() { 169 return readEnergyInfoNative(); 170 } 171 factoryReset()172 boolean factoryReset() { 173 return factoryResetNative(); 174 } 175 dump(FileDescriptor fd, String[] arguments)176 void dump(FileDescriptor fd, String[] arguments) { 177 dumpNative(fd, arguments); 178 } 179 dumpMetrics()180 byte[] dumpMetrics() { 181 return dumpMetricsNative(); 182 } 183 obfuscateAddress(byte[] address)184 byte[] obfuscateAddress(byte[] address) { 185 return obfuscateAddressNative(address); 186 } 187 setBufferLengthMillis(int codec, int value)188 boolean setBufferLengthMillis(int codec, int value) { 189 return setBufferLengthMillisNative(codec, value); 190 } 191 getMetricId(byte[] address)192 int getMetricId(byte[] address) { 193 return getMetricIdNative(address); 194 } 195 connectSocket(byte[] address, int type, byte[] uuid, int port, int flag, int callingUid)196 int connectSocket(byte[] address, int type, byte[] uuid, int port, int flag, int callingUid) { 197 return connectSocketNative(address, type, uuid, port, flag, callingUid); 198 } 199 createSocketChannel( int type, String serviceName, byte[] uuid, int port, int flag, int callingUid)200 int createSocketChannel( 201 int type, String serviceName, byte[] uuid, int port, int flag, int callingUid) { 202 return createSocketChannelNative(type, serviceName, uuid, port, flag, callingUid); 203 } 204 requestMaximumTxDataLength(byte[] address)205 void requestMaximumTxDataLength(byte[] address) { 206 requestMaximumTxDataLengthNative(address); 207 } 208 allowLowLatencyAudio(boolean allowed, byte[] address)209 boolean allowLowLatencyAudio(boolean allowed, byte[] address) { 210 return allowLowLatencyAudioNative(allowed, address); 211 } 212 metadataChanged(byte[] address, int key, byte[] value)213 void metadataChanged(byte[] address, int key, byte[] value) { 214 metadataChangedNative(address, key, value); 215 } 216 interopMatchAddr(String featureName, String address)217 boolean interopMatchAddr(String featureName, String address) { 218 return interopMatchAddrNative(featureName, address); 219 } 220 interopMatchName(String featureName, String name)221 boolean interopMatchName(String featureName, String name) { 222 return interopMatchNameNative(featureName, name); 223 } 224 interopMatchAddrOrName(String featureName, String address)225 boolean interopMatchAddrOrName(String featureName, String address) { 226 return interopMatchAddrOrNameNative(featureName, address); 227 } 228 interopDatabaseAddRemoveAddr( boolean doAdd, String featureName, String address, int length)229 void interopDatabaseAddRemoveAddr( 230 boolean doAdd, String featureName, String address, int length) { 231 interopDatabaseAddRemoveAddrNative(doAdd, featureName, address, length); 232 } 233 interopDatabaseAddRemoveName(boolean doAdd, String featureName, String name)234 void interopDatabaseAddRemoveName(boolean doAdd, String featureName, String name) { 235 interopDatabaseAddRemoveNameNative(doAdd, featureName, name); 236 } 237 getRemotePbapPceVersion(String address)238 int getRemotePbapPceVersion(String address) { 239 return getRemotePbapPceVersionNative(address); 240 } 241 pbapPseDynamicVersionUpgradeIsEnabled()242 boolean pbapPseDynamicVersionUpgradeIsEnabled() { 243 return pbapPseDynamicVersionUpgradeIsEnabledNative(); 244 } 245 isLogRedactionEnabled()246 boolean isLogRedactionEnabled() { 247 return isLogRedactionEnabledNative(); 248 } 249 getSocketL2capLocalChannelId(ParcelUuid connectionUuid)250 int getSocketL2capLocalChannelId(ParcelUuid connectionUuid) { 251 return getSocketL2capLocalChannelIdNative( 252 connectionUuid.getUuid().getLeastSignificantBits(), 253 connectionUuid.getUuid().getMostSignificantBits()); 254 } 255 getSocketL2capRemoteChannelId(ParcelUuid connectionUuid)256 int getSocketL2capRemoteChannelId(ParcelUuid connectionUuid) { 257 return getSocketL2capRemoteChannelIdNative( 258 connectionUuid.getUuid().getLeastSignificantBits(), 259 connectionUuid.getUuid().getMostSignificantBits()); 260 } 261 262 /**********************************************************************************************/ 263 /*********************************** callbacks from native ************************************/ 264 /**********************************************************************************************/ 265 266 // See JniCallbacks.java 267 268 /**********************************************************************************************/ 269 /******************************************* native *******************************************/ 270 /**********************************************************************************************/ 271 initNative( boolean startRestricted, boolean isCommonCriteriaMode, int configCompareResult, String[] initFlags, boolean isAtvDevice, String userDataDirectory)272 private native boolean initNative( 273 boolean startRestricted, 274 boolean isCommonCriteriaMode, 275 int configCompareResult, 276 String[] initFlags, 277 boolean isAtvDevice, 278 String userDataDirectory); 279 cleanupNative()280 private native void cleanupNative(); 281 enableNative()282 private native boolean enableNative(); 283 disableNative()284 private native boolean disableNative(); 285 setAdapterPropertyNative(int type, byte[] val)286 private native boolean setAdapterPropertyNative(int type, byte[] val); 287 getAdapterPropertiesNative()288 private native boolean getAdapterPropertiesNative(); 289 getAdapterPropertyNative(int type)290 private native boolean getAdapterPropertyNative(int type); 291 setDevicePropertyNative(byte[] address, int type, byte[] val)292 private native boolean setDevicePropertyNative(byte[] address, int type, byte[] val); 293 getDevicePropertyNative(byte[] address, int type)294 private native boolean getDevicePropertyNative(byte[] address, int type); 295 createBondNative(byte[] address, int addressType, int transport)296 private native boolean createBondNative(byte[] address, int addressType, int transport); 297 createBondOutOfBandNative( byte[] address, int transport, OobData p192Data, OobData p256Data)298 private native boolean createBondOutOfBandNative( 299 byte[] address, int transport, OobData p192Data, OobData p256Data); 300 removeBondNative(byte[] address)301 private native boolean removeBondNative(byte[] address); 302 cancelBondNative(byte[] address)303 private native boolean cancelBondNative(byte[] address); 304 pairingIsBusyNative()305 private native boolean pairingIsBusyNative(); 306 generateLocalOobDataNative(int transport)307 private native void generateLocalOobDataNative(int transport); 308 sdpSearchNative(byte[] address, byte[] uuid)309 private native boolean sdpSearchNative(byte[] address, byte[] uuid); 310 getConnectionStateNative(byte[] address)311 private native int getConnectionStateNative(byte[] address); 312 startDiscoveryNative()313 private native boolean startDiscoveryNative(); 314 cancelDiscoveryNative()315 private native boolean cancelDiscoveryNative(); 316 pinReplyNative(byte[] address, boolean accept, int len, byte[] pin)317 private native boolean pinReplyNative(byte[] address, boolean accept, int len, byte[] pin); 318 sspReplyNative(byte[] address, int type, boolean accept, int passkey)319 private native boolean sspReplyNative(byte[] address, int type, boolean accept, int passkey); 320 getRemoteServicesNative(byte[] address, int transport)321 private native boolean getRemoteServicesNative(byte[] address, int transport); 322 getRemoteMasInstancesNative(byte[] address)323 private native boolean getRemoteMasInstancesNative(byte[] address); 324 readEnergyInfoNative()325 private native int readEnergyInfoNative(); 326 factoryResetNative()327 private native boolean factoryResetNative(); 328 dumpNative(FileDescriptor fd, String[] arguments)329 private native void dumpNative(FileDescriptor fd, String[] arguments); 330 dumpMetricsNative()331 private native byte[] dumpMetricsNative(); 332 obfuscateAddressNative(byte[] address)333 private native byte[] obfuscateAddressNative(byte[] address); 334 setBufferLengthMillisNative(int codec, int value)335 private native boolean setBufferLengthMillisNative(int codec, int value); 336 getMetricIdNative(byte[] address)337 private native int getMetricIdNative(byte[] address); 338 connectSocketNative( byte[] address, int type, byte[] uuid, int port, int flag, int callingUid)339 private native int connectSocketNative( 340 byte[] address, int type, byte[] uuid, int port, int flag, int callingUid); 341 createSocketChannelNative( int type, String serviceName, byte[] uuid, int port, int flag, int callingUid)342 private native int createSocketChannelNative( 343 int type, String serviceName, byte[] uuid, int port, int flag, int callingUid); 344 requestMaximumTxDataLengthNative(byte[] address)345 private native void requestMaximumTxDataLengthNative(byte[] address); 346 allowLowLatencyAudioNative(boolean allowed, byte[] address)347 private native boolean allowLowLatencyAudioNative(boolean allowed, byte[] address); 348 metadataChangedNative(byte[] address, int key, byte[] value)349 private native void metadataChangedNative(byte[] address, int key, byte[] value); 350 interopMatchAddrNative(String featureName, String address)351 private native boolean interopMatchAddrNative(String featureName, String address); 352 interopMatchNameNative(String featureName, String name)353 private native boolean interopMatchNameNative(String featureName, String name); 354 interopMatchAddrOrNameNative(String featureName, String address)355 private native boolean interopMatchAddrOrNameNative(String featureName, String address); 356 interopDatabaseAddRemoveAddrNative( boolean doAdd, String featureName, String address, int length)357 private native void interopDatabaseAddRemoveAddrNative( 358 boolean doAdd, String featureName, String address, int length); 359 interopDatabaseAddRemoveNameNative( boolean doAdd, String featureName, String name)360 private native void interopDatabaseAddRemoveNameNative( 361 boolean doAdd, String featureName, String name); 362 getRemotePbapPceVersionNative(String address)363 private native int getRemotePbapPceVersionNative(String address); 364 pbapPseDynamicVersionUpgradeIsEnabledNative()365 private native boolean pbapPseDynamicVersionUpgradeIsEnabledNative(); 366 isLogRedactionEnabledNative()367 private native boolean isLogRedactionEnabledNative(); 368 getSocketL2capLocalChannelIdNative( long connectionUuidLsb, long connectionUuidMsb)369 private native int getSocketL2capLocalChannelIdNative( 370 long connectionUuidLsb, long connectionUuidMsb); 371 getSocketL2capRemoteChannelIdNative( long connectionUuidLsb, long connectionUuidMsb)372 private native int getSocketL2capRemoteChannelIdNative( 373 long connectionUuidLsb, long connectionUuidMsb); 374 } 375