Merge "Add BluetoothGatt, BluetoothDevice, BluetoothGattCallback."
diff --git a/nearby/service/java/com/android/server/nearby/common/bluetooth/BluetoothException.java b/nearby/service/java/com/android/server/nearby/common/bluetooth/BluetoothException.java
index 83d4a23..db2e1cc 100644
--- a/nearby/service/java/com/android/server/nearby/common/bluetooth/BluetoothException.java
+++ b/nearby/service/java/com/android/server/nearby/common/bluetooth/BluetoothException.java
@@ -20,10 +20,12 @@
* {@link Exception} thrown during a Bluetooth operation.
*/
public class BluetoothException extends Exception {
+ /** Constructor. */
public BluetoothException(String message) {
super(message);
}
+ /** Constructor. */
public BluetoothException(String message, Throwable throwable) {
super(message, throwable);
}
diff --git a/nearby/service/java/com/android/server/nearby/common/bluetooth/BluetoothGattException.java b/nearby/service/java/com/android/server/nearby/common/bluetooth/BluetoothGattException.java
new file mode 100644
index 0000000..5ac4882
--- /dev/null
+++ b/nearby/service/java/com/android/server/nearby/common/bluetooth/BluetoothGattException.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.nearby.common.bluetooth;
+
+/**
+ * Exception for Bluetooth GATT operations.
+ */
+public class BluetoothGattException extends BluetoothException {
+ private final int mErrorCode;
+
+ /** Constructor. */
+ public BluetoothGattException(String message, int errorCode) {
+ super(message);
+ mErrorCode = errorCode;
+ }
+
+ /** Constructor. */
+ public BluetoothGattException(String message, int errorCode, Throwable cause) {
+ super(message, cause);
+ mErrorCode = errorCode;
+ }
+
+ /** Returns Gatt error code. */
+ public int getGattErrorCode() {
+ return mErrorCode;
+ }
+}
diff --git a/nearby/service/java/com/android/server/nearby/common/bluetooth/BluetoothTimeoutException.java b/nearby/service/java/com/android/server/nearby/common/bluetooth/BluetoothTimeoutException.java
new file mode 100644
index 0000000..30fd188
--- /dev/null
+++ b/nearby/service/java/com/android/server/nearby/common/bluetooth/BluetoothTimeoutException.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.nearby.common.bluetooth;
+
+/**
+ * {@link Exception} thrown during a Bluetooth operation when a timeout occurs.
+ */
+public class BluetoothTimeoutException extends BluetoothException {
+
+ /** Constructor. */
+ public BluetoothTimeoutException(String message) {
+ super(message);
+ }
+
+ /** Constructor. */
+ public BluetoothTimeoutException(String message, Throwable throwable) {
+ super(message, throwable);
+ }
+}
diff --git a/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/AesEcbSingleBlockEncryption.java b/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/AesEcbSingleBlockEncryption.java
new file mode 100644
index 0000000..547931e
--- /dev/null
+++ b/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/AesEcbSingleBlockEncryption.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.nearby.common.bluetooth.fastpair;
+
+import android.annotation.SuppressLint;
+
+import java.security.GeneralSecurityException;
+import java.security.NoSuchAlgorithmException;
+
+import javax.crypto.Cipher;
+import javax.crypto.KeyGenerator;
+import javax.crypto.spec.SecretKeySpec;
+
+/**
+ * Utilities used for encrypting and decrypting Fast Pair packets.
+ */
+// SuppressLint for ""ecb encryption mode should not be used".
+// Reasons:
+// 1. FastPair data is guaranteed to be only 1 AES block in size, ECB is secure.
+// 2. In each case, the encrypted data is less than 16-bytes and is
+// padded up to 16-bytes using random data to fill the rest of the byte array,
+// so the plaintext will never be the same.
+@SuppressLint("GetInstance")
+public final class AesEcbSingleBlockEncryption {
+
+ public static final int AES_BLOCK_LENGTH = 16;
+ public static final int KEY_LENGTH = 16;
+
+ private AesEcbSingleBlockEncryption() {
+ }
+
+ /**
+ * Generates a 16-byte AES key.
+ */
+ public static byte[] generateKey() throws NoSuchAlgorithmException {
+ KeyGenerator generator = KeyGenerator.getInstance("AES");
+ generator.init(KEY_LENGTH * 8); // Ensure a 16-byte key is always used.
+ return generator.generateKey().getEncoded();
+ }
+
+ /**
+ * Encrypts data with the provided secret.
+ */
+ public static byte[] encrypt(byte[] secret, byte[] data) throws GeneralSecurityException {
+ return doEncryption(Cipher.ENCRYPT_MODE, secret, data);
+ }
+
+ /**
+ * Decrypts data with the provided secret.
+ */
+ public static byte[] decrypt(byte[] secret, byte[] data) throws GeneralSecurityException {
+ return doEncryption(Cipher.DECRYPT_MODE, secret, data);
+ }
+
+ private static byte[] doEncryption(int mode, byte[] secret, byte[] data)
+ throws GeneralSecurityException {
+ if (data.length != AES_BLOCK_LENGTH) {
+ throw new IllegalArgumentException("This encrypter only supports 16-byte inputs.");
+ }
+ Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
+ cipher.init(mode, new SecretKeySpec(secret, "AES"));
+ return cipher.doFinal(data);
+ }
+}
diff --git a/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/FastPairConstants.java b/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/FastPairConstants.java
new file mode 100644
index 0000000..0ff1bf2
--- /dev/null
+++ b/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/FastPairConstants.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.nearby.common.bluetooth.fastpair;
+
+import android.bluetooth.BluetoothDevice;
+
+/** Constants to share with other team. */
+public class FastPairConstants {
+ private static final String PACKAGE_NAME = "com.android.server.nearby";
+ private static final String PREFIX = PACKAGE_NAME + ".common.bluetooth.fastpair.";
+
+ /** MODEL_ID item name for extended intent field. */
+ public static final String EXTRA_MODEL_ID = PREFIX + "MODEL_ID";
+ /** CONNECTION_ID item name for extended intent field. */
+ public static final String EXTRA_CONNECTION_ID = PREFIX + "CONNECTION_ID";
+ /** BLUETOOTH_MAC_ADDRESS item name for extended intent field. */
+ public static final String EXTRA_BLUETOOTH_MAC_ADDRESS = PREFIX + "BLUETOOTH_MAC_ADDRESS";
+ /** COMPANION_SCAN_ITEM item name for extended intent field. */
+ public static final String EXTRA_SCAN_ITEM = PREFIX + "COMPANION_SCAN_ITEM";
+ /** BOND_RESULT item name for extended intent field. */
+ public static final String EXTRA_BOND_RESULT = PREFIX + "EXTRA_BOND_RESULT";
+
+ /**
+ * The bond result of the {@link BluetoothDevice} when FastPair launches the companion app, it
+ * means device is BONDED but the pairing process is not triggered by FastPair.
+ */
+ public static final int BOND_RESULT_SUCCESS_WITHOUT_FP = 0;
+
+ /**
+ * The bond result of the {@link BluetoothDevice} when FastPair launches the companion app, it
+ * means device is BONDED and the pairing process is triggered by FastPair.
+ */
+ public static final int BOND_RESULT_SUCCESS_WITH_FP = 1;
+
+ /**
+ * The bond result of the {@link BluetoothDevice} when FastPair launches the companion app, it
+ * means the pairing process triggered by FastPair is failed due to the lack of PIN code.
+ */
+ public static final int BOND_RESULT_FAIL_WITH_FP_WITHOUT_PIN = 2;
+
+ /**
+ * The bond result of the {@link BluetoothDevice} when FastPair launches the companion app, it
+ * means the pairing process triggered by FastPair is failed due to the PIN code is not
+ * confirmed by the user.
+ */
+ public static final int BOND_RESULT_FAIL_WITH_FP_WITH_PIN_NOT_CONFIRMED = 3;
+
+ /**
+ * The bond result of the {@link BluetoothDevice} when FastPair launches the companion app, it
+ * means the pairing process triggered by FastPair is failed due to the user thinks the PIN is
+ * wrong.
+ */
+ public static final int BOND_RESULT_FAIL_WITH_FP_WITH_PIN_WRONG = 4;
+
+ /**
+ * The bond result of the {@link BluetoothDevice} when FastPair launches the companion app, it
+ * means the pairing process triggered by FastPair is failed even after the user confirmed the
+ * PIN code is correct.
+ */
+ public static final int BOND_RESULT_FAIL_WITH_FP_WITH_PIN_CORRECT = 5;
+
+ private FastPairConstants() {}
+}
diff --git a/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/SignalLostException.java b/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/SignalLostException.java
new file mode 100644
index 0000000..244ee66
--- /dev/null
+++ b/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/SignalLostException.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.nearby.common.bluetooth.fastpair;
+
+/** Base class for fast pair signal lost exceptions. */
+public class SignalLostException extends PairingException {
+ SignalLostException(String message, Exception e) {
+ super(message);
+ initCause(e);
+ }
+}
diff --git a/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/SignalRotatedException.java b/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/SignalRotatedException.java
new file mode 100644
index 0000000..d0d2a5d
--- /dev/null
+++ b/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/SignalRotatedException.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.nearby.common.bluetooth.fastpair;
+
+/** Base class for fast pair signal rotated exceptions. */
+public class SignalRotatedException extends PairingException {
+ private final String mNewAddress;
+
+ SignalRotatedException(String message, String newAddress, Exception e) {
+ super(message);
+ this.mNewAddress = newAddress;
+ initCause(e);
+ }
+
+ /** Returns the new BLE address for the model ID. */
+ public String getNewAddress() {
+ return mNewAddress;
+ }
+}
diff --git a/nearby/service/java/com/android/server/nearby/common/bluetooth/testability/NonnullProvider.java b/nearby/service/java/com/android/server/nearby/common/bluetooth/testability/NonnullProvider.java
new file mode 100644
index 0000000..16abd99
--- /dev/null
+++ b/nearby/service/java/com/android/server/nearby/common/bluetooth/testability/NonnullProvider.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.nearby.common.bluetooth.testability;
+
+/**
+ * Provider that returns non-null instances.
+ *
+ * @param <T> Type of provided instance.
+ */
+public interface NonnullProvider<T> {
+ /** Get a non-null instance. */
+ T get();
+}
diff --git a/nearby/tests/src/com/android/server/nearby/common/bluetooth/fastpair/AesEcbSingleBlockEncryptionTest.java b/nearby/tests/src/com/android/server/nearby/common/bluetooth/fastpair/AesEcbSingleBlockEncryptionTest.java
new file mode 100644
index 0000000..0e13133
--- /dev/null
+++ b/nearby/tests/src/com/android/server/nearby/common/bluetooth/fastpair/AesEcbSingleBlockEncryptionTest.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.nearby.common.bluetooth.fastpair;
+
+import static com.google.common.io.BaseEncoding.base16;
+import static com.google.common.truth.Truth.assertThat;
+
+import android.platform.test.annotations.Presubmit;
+
+import androidx.test.filters.SmallTest;
+import androidx.test.runner.AndroidJUnit4;
+
+import com.google.common.primitives.Bytes;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/** Unit tests for {@link AesEcbSingleBlockEncryption}. */
+@Presubmit
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class AesEcbSingleBlockEncryptionTest {
+
+ private static final byte[] PLAINTEXT = base16().decode("F30F4E786C59A7BBF3873B5A49BA97EA");
+
+ @Test
+ public void encryptDecryptSuccessful() throws Exception {
+ byte[] secret = AesEcbSingleBlockEncryption.generateKey();
+ byte[] encrypted = AesEcbSingleBlockEncryption.encrypt(secret, PLAINTEXT);
+ assertThat(encrypted).isNotEqualTo(PLAINTEXT);
+ byte[] decrypted = AesEcbSingleBlockEncryption.decrypt(secret, encrypted);
+ assertThat(decrypted).isEqualTo(PLAINTEXT);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void encryptionSizeLimitationEnforced() throws Exception {
+ byte[] secret = AesEcbSingleBlockEncryption.generateKey();
+ byte[] largePacket = Bytes.concat(PLAINTEXT, PLAINTEXT);
+ AesEcbSingleBlockEncryption.encrypt(secret, largePacket);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void decryptionSizeLimitationEnforced() throws Exception {
+ byte[] secret = AesEcbSingleBlockEncryption.generateKey();
+ byte[] largePacket = Bytes.concat(PLAINTEXT, PLAINTEXT);
+ AesEcbSingleBlockEncryption.decrypt(secret, largePacket);
+ }
+}