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