Extract test support codes out from snippet to library.
The test support codes like Fast Pair provider simulator and
snippet helper are moved to test_support folder to make test
codes structure more clear. This change creates the following
two folders under tests/multidevices/clients/test_support:
1. fastpair_provider: fast Pair provider simulator library
2. snippet_helper: Mobly snippet event helper library
Test: atest -v CtsSeekerDiscoverProviderTest
BUG: 216079944
Change-Id: I7faccd4cec00a74d59bf0bcfa8316013d169e82e
diff --git a/nearby/tests/multidevices/clients/Android.bp b/nearby/tests/multidevices/clients/Android.bp
index 5e0ca15..e3c8bb1 100644
--- a/nearby/tests/multidevices/clients/Android.bp
+++ b/nearby/tests/multidevices/clients/Android.bp
@@ -24,15 +24,12 @@
],
sdk_version: "test_current",
static_libs: [
- "NearbyMultiDevicesClientsFastPairLiteProtos",
+ "MoblySnippetHelperLib",
+ "NearbyFastPairProviderLib",
"androidx.test.core",
- "error_prone_annotations",
- "fast-pair-lite-protos",
- "framework-annotations-lib",
"gson-prebuilt-jar",
"kotlin-stdlib",
"mobly-snippet-lib",
- "service-nearby",
],
}
diff --git a/nearby/tests/multidevices/clients/proguard.flags b/nearby/tests/multidevices/clients/proguard.flags
index 2e34dce..fd494a8 100644
--- a/nearby/tests/multidevices/clients/proguard.flags
+++ b/nearby/tests/multidevices/clients/proguard.flags
@@ -4,7 +4,7 @@
}
# Keep simulator reflection callback.
--keep class com.android.server.nearby.common.bluetooth.fastpair.testing.** {
+-keep class android.nearby.fastpair.provider.** {
*;
}
diff --git a/nearby/tests/multidevices/clients/src/android/nearby/multidevices/fastpair/provider/FastPairProviderSimulatorSnippet.kt b/nearby/tests/multidevices/clients/src/android/nearby/multidevices/fastpair/provider/FastPairProviderSimulatorSnippet.kt
index a2f50b4..a03085c 100644
--- a/nearby/tests/multidevices/clients/src/android/nearby/multidevices/fastpair/provider/FastPairProviderSimulatorSnippet.kt
+++ b/nearby/tests/multidevices/clients/src/android/nearby/multidevices/fastpair/provider/FastPairProviderSimulatorSnippet.kt
@@ -21,7 +21,7 @@
import android.content.Context
import android.os.Build
import androidx.test.platform.app.InstrumentationRegistry
-import com.android.server.nearby.common.bluetooth.fastpair.testing.FastPairSimulator
+import android.nearby.fastpair.provider.FastPairSimulator
import com.google.android.mobly.snippet.Snippet
import com.google.android.mobly.snippet.rpc.AsyncRpc
import com.google.android.mobly.snippet.rpc.Rpc
@@ -70,7 +70,9 @@
fastPairSimulator =
FastPairSimulator(
context,
- FastPairSimulator.Options.builder(modelId)
+ FastPairSimulator.Options.builder(
+ modelId
+ )
.setAdvertisingModelId(modelId)
.setBluetoothAddress(null)
.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
diff --git a/nearby/tests/multidevices/clients/src/android/nearby/multidevices/fastpair/provider/ProviderStatusEvents.kt b/nearby/tests/multidevices/clients/src/android/nearby/multidevices/fastpair/provider/ProviderStatusEvents.kt
index 20c8e85..eef4b8b 100644
--- a/nearby/tests/multidevices/clients/src/android/nearby/multidevices/fastpair/provider/ProviderStatusEvents.kt
+++ b/nearby/tests/multidevices/clients/src/android/nearby/multidevices/fastpair/provider/ProviderStatusEvents.kt
@@ -16,7 +16,7 @@
package android.nearby.multidevices.fastpair.provider
-import android.nearby.multidevices.common.postSnippetEvent
+import com.google.android.mobly.snippet.util.postSnippetEvent
/** The Mobly snippet events to report to the Python side. */
class ProviderStatusEvents(private val callbackId: String) :
diff --git a/nearby/tests/multidevices/clients/src/android/nearby/multidevices/fastpair/seeker/ScanCallbackEvents.kt b/nearby/tests/multidevices/clients/src/android/nearby/multidevices/fastpair/seeker/ScanCallbackEvents.kt
index 55a6b8f..5385238 100644
--- a/nearby/tests/multidevices/clients/src/android/nearby/multidevices/fastpair/seeker/ScanCallbackEvents.kt
+++ b/nearby/tests/multidevices/clients/src/android/nearby/multidevices/fastpair/seeker/ScanCallbackEvents.kt
@@ -18,7 +18,7 @@
import android.nearby.NearbyDevice
import android.nearby.ScanCallback
-import android.nearby.multidevices.common.postSnippetEvent
+import com.google.android.mobly.snippet.util.postSnippetEvent
/** The Mobly snippet events to report to the Python side. */
class ScanCallbackEvents(private val callbackId: String) : ScanCallback {
diff --git a/nearby/tests/multidevices/clients/proto/Android.bp b/nearby/tests/multidevices/clients/test_support/fastpair_provider/Android.bp
similarity index 62%
copy from nearby/tests/multidevices/clients/proto/Android.bp
copy to nearby/tests/multidevices/clients/test_support/fastpair_provider/Android.bp
index 80e09b4..dc3a919 100644
--- a/nearby/tests/multidevices/clients/proto/Android.bp
+++ b/nearby/tests/multidevices/clients/test_support/fastpair_provider/Android.bp
@@ -16,15 +16,20 @@
default_applicable_licenses: ["Android-Apache-2.0"],
}
-java_library {
- name: "NearbyMultiDevicesClientsFastPairLiteProtos",
- proto: {
- type: "lite",
- canonical_path_from_root: false,
- },
- sdk_version: "system_current",
- min_sdk_version: "30",
- srcs: ["src/*/*.proto"],
+android_library {
+ name: "NearbyFastPairProviderLib",
+ srcs: [
+ "src/**/*.java",
+ "src/**/*.kt",
+ ],
+ sdk_version: "test_current",
+ static_libs: [
+ "NearbyFastPairProviderLiteProtos",
+ "androidx.test.core",
+ "error_prone_annotations",
+ "fast-pair-lite-protos",
+ "framework-annotations-lib",
+ "kotlin-stdlib",
+ "service-nearby",
+ ],
}
-
-
diff --git a/nearby/tests/multidevices/clients/test_support/fastpair_provider/AndroidManifest.xml b/nearby/tests/multidevices/clients/test_support/fastpair_provider/AndroidManifest.xml
new file mode 100644
index 0000000..400a434
--- /dev/null
+++ b/nearby/tests/multidevices/clients/test_support/fastpair_provider/AndroidManifest.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ ~ Copyright (C) 2022 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.
+ -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="android.nearby.fastpair.provider">
+
+ <uses-feature android:name="android.hardware.bluetooth" />
+ <uses-feature android:name="android.hardware.bluetooth_le" />
+ <uses-permission android:name="android.permission.BLUETOOTH" />
+ <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
+ <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
+ <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
+ <uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
+ <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
+ <uses-permission android:name="android.permission.INTERNET"/>
+ <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
+
+</manifest>
diff --git a/nearby/tests/multidevices/clients/proto/Android.bp b/nearby/tests/multidevices/clients/test_support/fastpair_provider/proto/Android.bp
similarity index 90%
rename from nearby/tests/multidevices/clients/proto/Android.bp
rename to nearby/tests/multidevices/clients/test_support/fastpair_provider/proto/Android.bp
index 80e09b4..7ae43e5 100644
--- a/nearby/tests/multidevices/clients/proto/Android.bp
+++ b/nearby/tests/multidevices/clients/test_support/fastpair_provider/proto/Android.bp
@@ -17,14 +17,14 @@
}
java_library {
- name: "NearbyMultiDevicesClientsFastPairLiteProtos",
+ name: "NearbyFastPairProviderLiteProtos",
proto: {
type: "lite",
canonical_path_from_root: false,
},
sdk_version: "system_current",
min_sdk_version: "30",
- srcs: ["src/*/*.proto"],
+ srcs: ["*.proto"],
}
diff --git a/nearby/tests/multidevices/clients/proto/src/fastpair/event_stream_protocol.proto b/nearby/tests/multidevices/clients/test_support/fastpair_provider/proto/event_stream_protocol.proto
similarity index 94%
rename from nearby/tests/multidevices/clients/proto/src/fastpair/event_stream_protocol.proto
rename to nearby/tests/multidevices/clients/test_support/fastpair_provider/proto/event_stream_protocol.proto
index 69ed1ea..54db34a 100644
--- a/nearby/tests/multidevices/clients/proto/src/fastpair/event_stream_protocol.proto
+++ b/nearby/tests/multidevices/clients/test_support/fastpair_provider/proto/event_stream_protocol.proto
@@ -1,8 +1,8 @@
syntax = "proto2";
-package android.nearby.multidevices.fastpair;
+package android.nearby.fastpair.provider;
-option java_package = "android.nearby.multidevices.fastpair";
+option java_package = "android.nearby.fastpair.provider";
option java_outer_classname = "EventStreamProtocol";
enum EventGroup {
diff --git a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/FastPairAdvertiser.java b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/FastPairAdvertiser.java
similarity index 92%
rename from nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/FastPairAdvertiser.java
rename to nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/FastPairAdvertiser.java
index 33add27..95c077b 100644
--- a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/FastPairAdvertiser.java
+++ b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/FastPairAdvertiser.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package com.android.server.nearby.common.bluetooth.fastpair.testing;
+package android.nearby.fastpair.provider;
import androidx.annotation.Nullable;
diff --git a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/FastPairSimulator.java b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/FastPairSimulator.java
similarity index 76%
rename from nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/FastPairSimulator.java
rename to nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/FastPairSimulator.java
index cf8be76..4329c38 100644
--- a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/FastPairSimulator.java
+++ b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/FastPairSimulator.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package com.android.server.nearby.common.bluetooth.fastpair.testing;
+package android.nearby.fastpair.provider;
import static android.bluetooth.BluetoothAdapter.EXTRA_STATE;
import static android.bluetooth.BluetoothAdapter.SCAN_MODE_CONNECTABLE;
@@ -29,6 +29,8 @@
import static android.bluetooth.BluetoothGattCharacteristic.PROPERTY_NOTIFY;
import static android.bluetooth.BluetoothGattCharacteristic.PROPERTY_READ;
import static android.bluetooth.BluetoothGattCharacteristic.PROPERTY_WRITE;
+import static android.nearby.fastpair.provider.bluetooth.BluetoothManager.wrap;
+import static android.nearby.fastpair.provider.bluetooth.RfcommServer.State.CONNECTED;
import static com.android.server.nearby.common.bluetooth.fastpair.AesEcbSingleBlockEncryption.AES_BLOCK_LENGTH;
import static com.android.server.nearby.common.bluetooth.fastpair.AesEcbSingleBlockEncryption.encrypt;
@@ -37,8 +39,6 @@
import static com.android.server.nearby.common.bluetooth.fastpair.Constants.TransportDiscoveryService.BLUETOOTH_SIG_ORGANIZATION_ID;
import static com.android.server.nearby.common.bluetooth.fastpair.EllipticCurveDiffieHellmanExchange.PUBLIC_KEY_LENGTH;
import static com.android.server.nearby.common.bluetooth.fastpair.MessageStreamHmacEncoder.SECTION_NONCE_LENGTH;
-import static com.android.server.nearby.common.bluetooth.fastpair.testing.RfcommServer.State.CONNECTED;
-import static com.android.server.nearby.common.bluetooth.testability.android.bluetooth.BluetoothManager.wrap;
import static com.google.common.io.BaseEncoding.base16;
import static com.google.common.primitives.Bytes.concat;
@@ -56,12 +56,22 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
-import android.nearby.multidevices.fastpair.EventStreamProtocol.AcknowledgementEventCode;
-import android.nearby.multidevices.fastpair.EventStreamProtocol.DeviceActionEventCode;
-import android.nearby.multidevices.fastpair.EventStreamProtocol.DeviceCapabilitySyncEventCode;
-import android.nearby.multidevices.fastpair.EventStreamProtocol.DeviceConfigurationEventCode;
-import android.nearby.multidevices.fastpair.EventStreamProtocol.DeviceEventCode;
-import android.nearby.multidevices.fastpair.EventStreamProtocol.EventGroup;
+import android.nearby.fastpair.provider.EventStreamProtocol.AcknowledgementEventCode;
+import android.nearby.fastpair.provider.EventStreamProtocol.DeviceActionEventCode;
+import android.nearby.fastpair.provider.EventStreamProtocol.DeviceCapabilitySyncEventCode;
+import android.nearby.fastpair.provider.EventStreamProtocol.DeviceConfigurationEventCode;
+import android.nearby.fastpair.provider.EventStreamProtocol.DeviceEventCode;
+import android.nearby.fastpair.provider.EventStreamProtocol.EventGroup;
+import android.nearby.fastpair.provider.bluetooth.BluetoothGattServerConfig;
+import android.nearby.fastpair.provider.bluetooth.BluetoothGattServerConfig.ServiceConfig;
+import android.nearby.fastpair.provider.bluetooth.BluetoothGattServerConnection;
+import android.nearby.fastpair.provider.bluetooth.BluetoothGattServerConnection.Notifier;
+import android.nearby.fastpair.provider.bluetooth.BluetoothGattServerHelper;
+import android.nearby.fastpair.provider.bluetooth.BluetoothGattServlet;
+import android.nearby.fastpair.provider.bluetooth.RfcommServer;
+import android.nearby.fastpair.provider.crypto.Crypto;
+import android.nearby.fastpair.provider.crypto.E2eeCalculator;
+import android.nearby.fastpair.provider.utils.Logger;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Handler;
@@ -99,12 +109,6 @@
import com.android.server.nearby.common.bluetooth.fastpair.NamingEncoder;
import com.android.server.nearby.common.bluetooth.fastpair.Reflect;
import com.android.server.nearby.common.bluetooth.fastpair.ReflectionException;
-import com.android.server.nearby.common.bluetooth.gatt.server.BluetoothGattServerConfig;
-import com.android.server.nearby.common.bluetooth.gatt.server.BluetoothGattServerConfig.ServiceConfig;
-import com.android.server.nearby.common.bluetooth.gatt.server.BluetoothGattServerConnection;
-import com.android.server.nearby.common.bluetooth.gatt.server.BluetoothGattServerConnection.Notifier;
-import com.android.server.nearby.common.bluetooth.gatt.server.BluetoothGattServerHelper;
-import com.android.server.nearby.common.bluetooth.gatt.server.BluetoothGattServlet;
import com.google.common.base.Ascii;
import com.google.common.primitives.Bytes;
@@ -157,7 +161,7 @@
*/
public class FastPairSimulator {
private static final String TAG = "FastPairSimulator";
- private final Logger logger = new Logger(TAG);
+ private final Logger mLogger = new Logger(TAG);
/**
* Headphones. Generated by
@@ -207,11 +211,11 @@
* </ul>
* </ul>
*/
- private String deviceFirmwareVersion = "1.1.0";
+ private String mDeviceFirmwareVersion = "1.1.0";
- private byte[] sessionNonce;
+ private byte[] mSessionNonce;
- private boolean useLogFullEvent = true;
+ private boolean mUseLogFullEvent = true;
private enum ResultCode {
SUCCESS((byte) 0x00),
@@ -220,10 +224,10 @@
UNSUPPORTED_ORGANIZATION_ID((byte) 0x03),
OPERATION_FAILED((byte) 0x04);
- private final byte byteValue;
+ private final byte mByteValue;
ResultCode(byte byteValue) {
- this.byteValue = byteValue;
+ this.mByteValue = byteValue;
}
}
@@ -232,33 +236,33 @@
ON((byte) 0x01),
TEMPORARILY_UNAVAILABLE((byte) 0x10);
- private final byte byteValue;
+ private final byte mByteValue;
TransportState(byte byteValue) {
- this.byteValue = byteValue;
+ this.mByteValue = byteValue;
}
}
- private final Context context;
- private final Options options;
- private final Handler uiThreadHandler = new Handler(Looper.getMainLooper());
+ private final Context mContext;
+ private final Options mOptions;
+ private final Handler mUiThreadHandler = new Handler(Looper.getMainLooper());
// No thread pool: Only used in test app (outside gmscore) and in javatests/.../gmscore/.
- private final ScheduledExecutorService executor =
+ private final ScheduledExecutorService mExecutor =
Executors.newSingleThreadScheduledExecutor(); // exempt
- private final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- private final BroadcastReceiver broadcastReceiver =
+ private final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
+ private final BroadcastReceiver mBroadcastReceiver =
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
- if (shouldFailPairing) {
- logger.log("Pairing disabled by test app switch");
+ if (mShouldFailPairing) {
+ mLogger.log("Pairing disabled by test app switch");
return;
}
- if (isDestroyed) {
+ if (mIsDestroyed) {
// Sometimes this receiver does not successfully unregister in destroy()
// which causes events to occur after the simulator is stopped, so ignore
// those events.
- logger.log("Intent received after simulator destroyed, ignoring");
+ mLogger.log("Intent received after simulator destroyed, ignoring");
return;
}
BluetoothDevice device = intent.getParcelableExtra(
@@ -266,52 +270,52 @@
switch (intent.getAction()) {
case BluetoothAdapter.ACTION_SCAN_MODE_CHANGED:
if (isDiscoverable()) {
- isDiscoverableLatch.countDown();
+ mIsDiscoverableLatch.countDown();
}
break;
case BluetoothDevice.ACTION_PAIRING_REQUEST:
int variant = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
ERROR);
int key = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY, ERROR);
- logger.log(
+ mLogger.log(
"Pairing request, variant=%d, key=%s", variant,
key == ERROR ? "(none)" : key);
// Prevent Bluetooth Settings from getting the pairing request.
abortBroadcast();
- pairingDevice = device;
- if (secret == null) {
+ mPairingDevice = device;
+ if (mSecret == null) {
// We haven't done the handshake over GATT to agree on the shared
// secret. For now, just accept anyway (so we can still simulate
// old 1.0 model IDs).
- logger.log("No handshake, auto-accepting anyway.");
+ mLogger.log("No handshake, auto-accepting anyway.");
setPasskeyConfirmation(true);
} else if (variant
== BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION) {
// Store the passkey. And check it, since there's a race (see
// method for why). Usually this check is a no-op and we'll get
// the passkey later over GATT.
- localPasskey = key;
+ mLocalPasskey = key;
checkPasskey();
} else if (variant == PAIRING_VARIANT_DISPLAY_PASSKEY) {
- if (passkeyEventCallback != null) {
- passkeyEventCallback.onPasskeyRequested(
+ if (mPasskeyEventCallback != null) {
+ mPasskeyEventCallback.onPasskeyRequested(
FastPairSimulator.this::enterPassKey);
} else {
- logger.log("passkeyEventCallback is not set!");
+ mLogger.log("passkeyEventCallback is not set!");
enterPassKey(key);
}
} else if (variant == PAIRING_VARIANT_CONSENT) {
setPasskeyConfirmation(true);
} else if (variant == BluetoothDevice.PAIRING_VARIANT_PIN) {
- if (passkeyEventCallback != null) {
- passkeyEventCallback.onPasskeyRequested(
+ if (mPasskeyEventCallback != null) {
+ mPasskeyEventCallback.onPasskeyRequested(
(int pin) -> {
byte[] newPin = convertPinToBytes(
String.format(Locale.ENGLISH, "%d", pin));
- pairingDevice.setPin(newPin);
+ mPairingDevice.setPin(newPin);
});
}
} else {
@@ -324,36 +328,36 @@
int bondState =
intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
BluetoothDevice.BOND_NONE);
- logger.log("Bond state to %s changed to %d", device, bondState);
+ mLogger.log("Bond state to %s changed to %d", device, bondState);
switch (bondState) {
case BluetoothDevice.BOND_BONDING:
// If we've started bonding, we shouldn't be advertising.
- advertiser.stopAdvertising();
+ mAdvertiser.stopAdvertising();
// Not discoverable anymore, but still connectable.
setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE);
break;
case BluetoothDevice.BOND_BONDED:
// Once bonded, advertise the account keys.
- advertiser.startAdvertising(accountKeysServiceData());
+ mAdvertiser.startAdvertising(accountKeysServiceData());
setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE);
// If it is subsequent pair, we need to add paired device here.
- if (isSubsequentPair
- && secret != null
- && secret.length == AES_BLOCK_LENGTH) {
- addAccountKey(secret, pairingDevice);
+ if (mIsSubsequentPair
+ && mSecret != null
+ && mSecret.length == AES_BLOCK_LENGTH) {
+ addAccountKey(mSecret, mPairingDevice);
}
break;
case BluetoothDevice.BOND_NONE:
// If the bonding process fails, we should be advertising again.
- advertiser.startAdvertising(getServiceData());
+ mAdvertiser.startAdvertising(getServiceData());
break;
default:
break;
}
break;
case BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED:
- logger.log(
+ mLogger.log(
"Connection state to %s changed to %d",
device,
intent.getIntExtra(
@@ -362,7 +366,7 @@
break;
case BluetoothAdapter.ACTION_STATE_CHANGED:
int state = intent.getIntExtra(EXTRA_STATE, -1);
- logger.log("Bluetooth adapter state=%s", state);
+ mLogger.log("Bluetooth adapter state=%s", state);
switch (state) {
case STATE_ON:
startRfcommServer();
@@ -374,7 +378,7 @@
}
break;
default:
- logger.log(new IllegalArgumentException(intent.toString()),
+ mLogger.log(new IllegalArgumentException(intent.toString()),
"Received unexpected intent");
break;
}
@@ -394,7 +398,7 @@
return pinBytes;
}
- private final NotifiableGattServlet passkeyServlet =
+ private final NotifiableGattServlet mPasskeyServlet =
new NotifiableGattServlet() {
@Override
// Simulating deprecated API {@code PasskeyCharacteristic.ID} for testing.
@@ -409,28 +413,28 @@
@Override
public void write(
BluetoothGattServerConnection connection, int offset, byte[] value) {
- logger.log("Got value from passkey servlet: %s", base16().encode(value));
- if (secret == null) {
- logger.log("Ignoring write to passkey characteristic, no pairing secret.");
+ mLogger.log("Got value from passkey servlet: %s", base16().encode(value));
+ if (mSecret == null) {
+ mLogger.log("Ignoring write to passkey characteristic, no pairing secret.");
return;
}
try {
- remotePasskey = PasskeyCharacteristic.decrypt(
- PasskeyCharacteristic.Type.SEEKER, secret, value);
- if (passkeyEventCallback != null) {
- passkeyEventCallback.onRemotePasskeyReceived(remotePasskey);
+ mRemotePasskey = PasskeyCharacteristic.decrypt(
+ PasskeyCharacteristic.Type.SEEKER, mSecret, value);
+ if (mPasskeyEventCallback != null) {
+ mPasskeyEventCallback.onRemotePasskeyReceived(mRemotePasskey);
}
checkPasskey();
} catch (GeneralSecurityException e) {
- logger.log(
+ mLogger.log(
"Decrypting passkey value %s failed using key %s",
- base16().encode(value), base16().encode(secret));
+ base16().encode(value), base16().encode(mSecret));
}
}
};
- private final NotifiableGattServlet deviceNameServlet =
+ private final NotifiableGattServlet mDeviceNameServlet =
new NotifiableGattServlet() {
@Override
// Simulating deprecated API {@code NameCharacteristic.ID} for testing.
@@ -445,112 +449,112 @@
@Override
public void write(
BluetoothGattServerConnection connection, int offset, byte[] value) {
- logger.log("Got value from device naming servlet: %s", base16().encode(value));
- if (secret == null) {
- logger.log("Ignoring write to name characteristic, no pairing secret.");
+ mLogger.log("Got value from device naming servlet: %s", base16().encode(value));
+ if (mSecret == null) {
+ mLogger.log("Ignoring write to name characteristic, no pairing secret.");
return;
}
// Parse the device name from seeker to write name into provider.
- logger.log("Got name byte array size = %d", value.length);
+ mLogger.log("Got name byte array size = %d", value.length);
try {
String decryptedDeviceName =
- NamingEncoder.decodeNamingPacket(secret, value);
+ NamingEncoder.decodeNamingPacket(mSecret, value);
if (decryptedDeviceName != null) {
setDeviceName(decryptedDeviceName.getBytes(StandardCharsets.UTF_8));
- logger.log("write device name = %s", decryptedDeviceName);
+ mLogger.log("write device name = %s", decryptedDeviceName);
}
} catch (GeneralSecurityException e) {
- logger.log(e, "Failed to decrypt device name.");
+ mLogger.log(e, "Failed to decrypt device name.");
}
// For testing to make sure we get the new provider name from simulator.
- if (writeNameCountDown != null) {
- logger.log("finish count down latch to write device name.");
- writeNameCountDown.countDown();
+ if (mWriteNameCountDown != null) {
+ mLogger.log("finish count down latch to write device name.");
+ mWriteNameCountDown.countDown();
}
}
};
- private Value bluetoothAddress;
- private final FastPairAdvertiser advertiser;
+ private Value mBluetoothAddress;
+ private final FastPairAdvertiser mAdvertiser;
private final Map<String, BluetoothGattServerHelper> mBluetoothGattServerHelpers =
new HashMap<>();
- private CountDownLatch isDiscoverableLatch = new CountDownLatch(1);
- private ScheduledFuture<?> revertDiscoverableFuture;
- private boolean shouldFailPairing = false;
- private boolean isDestroyed = false;
- private boolean isAdvertising;
+ private CountDownLatch mIsDiscoverableLatch = new CountDownLatch(1);
+ private ScheduledFuture<?> mRevertDiscoverableFuture;
+ private boolean mShouldFailPairing = false;
+ private boolean mIsDestroyed = false;
+ private boolean mIsAdvertising;
@Nullable
- private String bleAddress;
- private BluetoothDevice pairingDevice;
- private int localPasskey;
- private int remotePasskey;
+ private String mBleAddress;
+ private BluetoothDevice mPairingDevice;
+ private int mLocalPasskey;
+ private int mRemotePasskey;
@Nullable
- private byte[] secret;
+ private byte[] mSecret;
@Nullable
- private byte[] accountKey; // The latest account key added.
+ private byte[] mAccountKey; // The latest account key added.
// The first account key added. Eddystone treats that account as the owner of the device.
@Nullable
- private byte[] ownerAccountKey;
+ private byte[] mOwnerAccountKey;
@Nullable
- private PasskeyConfirmationCallback passkeyConfirmationCallback;
+ private PasskeyConfirmationCallback mPasskeyConfirmationCallback;
@Nullable
- private DeviceNameCallback deviceNameCallback;
+ private DeviceNameCallback mDeviceNameCallback;
@Nullable
- private PasskeyEventCallback passkeyEventCallback;
- private final List<BatteryValue> batteryValues;
- private boolean suppressBatteryNotification = false;
- private boolean suppressSubsequentPairingNotification = false;
- HandshakeRequest handshakeRequest;
+ private PasskeyEventCallback mPasskeyEventCallback;
+ private final List<BatteryValue> mBatteryValues;
+ private boolean mSuppressBatteryNotification = false;
+ private boolean mSuppressSubsequentPairingNotification = false;
+ HandshakeRequest mHandshakeRequest;
@Nullable
- private CountDownLatch writeNameCountDown;
- private final RfcommServer rfcommServer = new RfcommServer();
- private final boolean dataOnlyConnection;
- private boolean supportDynamicBufferSize = false;
- private NotifiableGattServlet beaconActionsServlet;
- private final FastPairSimulatorDatabase fastPairSimulatorDatabase;
- private boolean isSubsequentPair = false;
+ private CountDownLatch mWriteNameCountDown;
+ private final RfcommServer mRfcommServer = new RfcommServer();
+ private final boolean mDataOnlyConnection;
+ private boolean mSupportDynamicBufferSize = false;
+ private NotifiableGattServlet mBeaconActionsServlet;
+ private final FastPairSimulatorDatabase mFastPairSimulatorDatabase;
+ private boolean mIsSubsequentPair = false;
/** Sets the flag for failing paring for debug purpose. */
public void setShouldFailPairing(boolean shouldFailPairing) {
- this.shouldFailPairing = shouldFailPairing;
+ this.mShouldFailPairing = shouldFailPairing;
}
/** Gets the flag for failing paring for debug purpose. */
public boolean getShouldFailPairing() {
- return shouldFailPairing;
+ return mShouldFailPairing;
}
/** Clear the battery values, then no battery information is packed when advertising. */
public void clearBatteryValues() {
- batteryValues.clear();
+ mBatteryValues.clear();
}
/** Sets the battery items which will be included in the advertisement packet. */
public void setBatteryValues(BatteryValue... batteryValues) {
- this.batteryValues.clear();
- Collections.addAll(this.batteryValues, batteryValues);
+ this.mBatteryValues.clear();
+ Collections.addAll(this.mBatteryValues, batteryValues);
}
/** Sets whether the battery advertisement packet is within suppress type or not. */
public void setSuppressBatteryNotification(boolean suppressBatteryNotification) {
- this.suppressBatteryNotification = suppressBatteryNotification;
+ this.mSuppressBatteryNotification = suppressBatteryNotification;
}
/** Sets whether the account key data is within suppress type or not. */
public void setSuppressSubsequentPairingNotification(boolean isSuppress) {
- suppressSubsequentPairingNotification = isSuppress;
+ mSuppressSubsequentPairingNotification = isSuppress;
}
/** Calls this to start advertising after some values are changed. */
public void startAdvertising() {
- advertiser.startAdvertising(getServiceData());
+ mAdvertiser.startAdvertising(getServiceData());
}
/** Send Event Message on to rfcomm connected devices. */
public void sendEventStreamMessageToRfcommDevices(EventGroup eventGroup) {
// Send fake log when event code is logging and type is not using Log_Full event.
- if (eventGroup == EventGroup.LOGGING && !useLogFullEvent) {
- rfcommServer.sendFakeEventStreamLoggingMessage(
+ if (eventGroup == EventGroup.LOGGING && !mUseLogFullEvent) {
+ mRfcommServer.sendFakeEventStreamLoggingMessage(
getDeviceName()
+ " "
+ getBleAddress()
@@ -558,12 +562,12 @@
+ new SimpleDateFormat("HH:mm:ss:SSS", Locale.US)
.format(Calendar.getInstance().getTime()));
} else {
- rfcommServer.sendFakeEventStreamMessage(eventGroup);
+ mRfcommServer.sendFakeEventStreamMessage(eventGroup);
}
}
public void setUseLogFullEvent(boolean useLogFullEvent) {
- this.useLogFullEvent = useLogFullEvent;
+ this.mUseLogFullEvent = useLogFullEvent;
}
/** An optional way to get status updates. */
@@ -787,7 +791,7 @@
private String mBluetoothAddress;
@Nullable
- private String mbleAddress;
+ private String mBleAddress;
private boolean mDataOnlyConnection;
@@ -824,7 +828,7 @@
this.mModelId = option.mModelId;
this.mAdvertisingModelId = option.mAdvertisingModelId;
this.mBluetoothAddress = option.mBluetoothAddress;
- this.mbleAddress = option.mBleAddress;
+ this.mBleAddress = option.mBleAddress;
this.mDataOnlyConnection = option.mDataOnlyConnection;
this.mTxPowerLevel = option.mTxPowerLevel;
this.mEnableNameCharacteristic = option.mEnableNameCharacteristic;
@@ -860,7 +864,7 @@
}
public Builder setBleAddress(@Nullable String bleAddress) {
- this.mbleAddress = bleAddress;
+ this.mBleAddress = bleAddress;
return this;
}
@@ -955,7 +959,7 @@
Ascii.toUpperCase(mModelId),
Ascii.toUpperCase(mAdvertisingModelId),
mBluetoothAddress,
- mbleAddress,
+ mBleAddress,
mDataOnlyConnection,
mTxPowerLevel,
mEnableNameCharacteristic,
@@ -974,10 +978,10 @@
}
public FastPairSimulator(Context context, Options options) {
- this.context = context;
- this.options = options;
+ this.mContext = context;
+ this.mOptions = options;
- this.batteryValues = new ArrayList<>();
+ this.mBatteryValues = new ArrayList<>();
String bluetoothAddress =
!TextUtils.isEmpty(options.getBluetoothAddress())
@@ -987,29 +991,29 @@
if (bluetoothAddress == null && VERSION.SDK_INT >= VERSION_CODES.O) {
// Requires a modified Android O build for access to bluetoothAdapter.getAddress().
// See http://google3/java/com/google/location/nearby/apps/fastpair/simulator/README.md.
- bluetoothAddress = bluetoothAdapter.getAddress();
+ bluetoothAddress = mBluetoothAdapter.getAddress();
}
- this.bluetoothAddress =
+ this.mBluetoothAddress =
new Value(BluetoothAddress.decode(bluetoothAddress), ByteOrder.BIG_ENDIAN);
- this.bleAddress = options.getBleAddress();
- this.dataOnlyConnection = options.getDataOnlyConnection();
- this.advertiser = new OreoFastPairAdvertiser(this);
+ this.mBleAddress = options.getBleAddress();
+ this.mDataOnlyConnection = options.getDataOnlyConnection();
+ this.mAdvertiser = new OreoFastPairAdvertiser(this);
- fastPairSimulatorDatabase = new FastPairSimulatorDatabase(context);
+ mFastPairSimulatorDatabase = new FastPairSimulatorDatabase(context);
byte[] deviceName = getDeviceNameInBytes();
- logger.log(
+ mLogger.log(
"Provider default device name is %s",
deviceName != null ? new String(deviceName, StandardCharsets.UTF_8) : null);
- if (dataOnlyConnection) {
+ if (mDataOnlyConnection) {
// To get BLE address, we need to start advertising first, and then
// {@code#setBleAddress} will be called with BLE address.
- advertiser.startAdvertising(modelIdServiceData(/* forAdvertising= */ true));
+ mAdvertiser.startAdvertising(modelIdServiceData(/* forAdvertising= */ true));
} else {
// Make this so that the simulator doesn't start automatically.
// This is tricky since the simulator is used in our integ tests as well.
- start(bleAddress != null ? bleAddress : bluetoothAddress);
+ start(mBleAddress != null ? mBleAddress : bluetoothAddress);
}
}
@@ -1019,23 +1023,22 @@
filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
- context.registerReceiver(broadcastReceiver, filter);
+ mContext.registerReceiver(mBroadcastReceiver, filter);
- BluetoothManager bluetoothManager =
- (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
+ BluetoothManager bluetoothManager = mContext.getSystemService(BluetoothManager.class);
BluetoothGattServerHelper bluetoothGattServerHelper =
- new BluetoothGattServerHelper(context, wrap(bluetoothManager));
+ new BluetoothGattServerHelper(mContext, wrap(bluetoothManager));
mBluetoothGattServerHelpers.put(address, bluetoothGattServerHelper);
- if (options.getBecomeDiscoverable()) {
+ if (mOptions.getBecomeDiscoverable()) {
try {
becomeDiscoverable();
} catch (InterruptedException | TimeoutException e) {
- logger.log(e, "Error becoming discoverable");
+ mLogger.log(e, "Error becoming discoverable");
}
}
- advertiser.startAdvertising(modelIdServiceData(/* forAdvertising= */ true));
+ mAdvertiser.startAdvertising(modelIdServiceData(/* forAdvertising= */ true));
startGattServer(bluetoothGattServerHelper);
startRfcommServer();
scheduleAdvertisingRefresh();
@@ -1047,16 +1050,16 @@
*/
@SuppressWarnings("FutureReturnValueIgnored")
private void scheduleAdvertisingRefresh() {
- executor.scheduleAtFixedRate(
+ mExecutor.scheduleAtFixedRate(
() -> {
- if (isAdvertising) {
- advertiser.startAdvertising(getServiceData());
+ if (mIsAdvertising) {
+ mAdvertiser.startAdvertising(getServiceData());
}
},
- options.getIsMemoryTest()
+ mOptions.getIsMemoryTest()
? ADVERTISING_REFRESH_DELAY_5_MINS
: ADVERTISING_REFRESH_DELAY_1_MIN,
- options.getIsMemoryTest()
+ mOptions.getIsMemoryTest()
? ADVERTISING_REFRESH_DELAY_5_MINS
: ADVERTISING_REFRESH_DELAY_1_MIN,
TimeUnit.MILLISECONDS);
@@ -1064,49 +1067,50 @@
public void destroy() {
try {
- logger.log("Destroying simulator");
- isDestroyed = true;
- context.unregisterReceiver(broadcastReceiver);
- advertiser.stopAdvertising();
+ mLogger.log("Destroying simulator");
+ mIsDestroyed = true;
+ mContext.unregisterReceiver(mBroadcastReceiver);
+ mAdvertiser.stopAdvertising();
for (BluetoothGattServerHelper helper : mBluetoothGattServerHelpers.values()) {
helper.close();
}
stopRfcommServer();
- deviceNameCallback = null;
- executor.shutdownNow();
+ mDeviceNameCallback = null;
+ mExecutor.shutdownNow();
} catch (IllegalArgumentException ignored) {
// Happens if you haven't given us permissions yet, so we didn't register the receiver.
}
}
public boolean isDestroyed() {
- return isDestroyed;
+ return mIsDestroyed;
}
@Nullable
public String getBluetoothAddress() {
- return BluetoothAddress.encode(bluetoothAddress.getBytes(ByteOrder.BIG_ENDIAN));
+ return BluetoothAddress.encode(mBluetoothAddress.getBytes(ByteOrder.BIG_ENDIAN));
}
public boolean isAdvertising() {
- return isAdvertising;
+ return mIsAdvertising;
}
public void setIsAdvertising(boolean isAdvertising) {
- if (this.isAdvertising != isAdvertising) {
- this.isAdvertising = isAdvertising;
- options.getCallback().onAdvertisingChanged();
+ if (this.mIsAdvertising != isAdvertising) {
+ this.mIsAdvertising = isAdvertising;
+ mOptions.getCallback().onAdvertisingChanged();
}
}
public void stopAdvertising() {
- advertiser.stopAdvertising();
+ mAdvertiser.stopAdvertising();
}
public void setBleAddress(String bleAddress) {
- this.bleAddress = bleAddress;
- if (dataOnlyConnection) {
- bluetoothAddress = new Value(BluetoothAddress.decode(bleAddress), ByteOrder.BIG_ENDIAN);
+ this.mBleAddress = bleAddress;
+ if (mDataOnlyConnection) {
+ mBluetoothAddress = new Value(BluetoothAddress.decode(bleAddress),
+ ByteOrder.BIG_ENDIAN);
start(bleAddress);
}
// When BLE address changes, needs to send BLE address to the client again.
@@ -1115,28 +1119,28 @@
// If we are advertising something other than the model id (e.g. the bloom filter), restart
// the advertisement so that it is updated with the new address.
if (isAdvertising() && !isDiscoverable()) {
- advertiser.startAdvertising(getServiceData());
+ mAdvertiser.startAdvertising(getServiceData());
}
}
@Nullable
public String getBleAddress() {
- return bleAddress;
+ return mBleAddress;
}
// This method is only for testing to make test block until write name success or time out.
@VisibleForTesting
public void setCountDownLatchToWriteName(CountDownLatch countDownLatch) {
- logger.log("Set up count down latch to write device name.");
- writeNameCountDown = countDownLatch;
+ mLogger.log("Set up count down latch to write device name.");
+ mWriteNameCountDown = countDownLatch;
}
public boolean areBeaconActionsNotificationsEnabled() {
- return beaconActionsServlet.areNotificationsEnabled();
+ return mBeaconActionsServlet.areNotificationsEnabled();
}
private abstract class NotifiableGattServlet extends BluetoothGattServlet {
- private final Map<BluetoothGattServerConnection, Notifier> connections = new HashMap<>();
+ private final Map<BluetoothGattServerConnection, Notifier> mConnections = new HashMap<>();
abstract BluetoothGattCharacteristic getBaseCharacteristic();
@@ -1155,39 +1159,39 @@
@Override
public void enableNotification(BluetoothGattServerConnection connection, Notifier notifier)
throws BluetoothGattException {
- logger.log("Registering notifier for %s", getCharacteristic());
- connections.put(connection, notifier);
+ mLogger.log("Registering notifier for %s", getCharacteristic());
+ mConnections.put(connection, notifier);
}
@Override
public void disableNotification(BluetoothGattServerConnection connection, Notifier notifier)
throws BluetoothGattException {
- logger.log("Removing notifier for %s", getCharacteristic());
- connections.remove(connection);
+ mLogger.log("Removing notifier for %s", getCharacteristic());
+ mConnections.remove(connection);
}
boolean areNotificationsEnabled() {
- return !connections.isEmpty();
+ return !mConnections.isEmpty();
}
void sendNotification(byte[] data) {
- if (connections.isEmpty()) {
- logger.log("Not sending notify as no notifier registered");
+ if (mConnections.isEmpty()) {
+ mLogger.log("Not sending notify as no notifier registered");
return;
}
// Needs to be on a separate thread to avoid deadlocking and timing out (waits for a
// callback from OS, which happens on the main thread).
- executor.execute(
+ mExecutor.execute(
() -> {
for (Map.Entry<BluetoothGattServerConnection, Notifier> entry :
- connections.entrySet()) {
+ mConnections.entrySet()) {
try {
- logger.log("Sending notify %s to %s",
+ mLogger.log("Sending notify %s to %s",
getCharacteristic(),
entry.getKey().getDevice().getAddress());
entry.getValue().notify(data);
} catch (BluetoothException e) {
- logger.log(
+ mLogger.log(
e,
"Failed to notify (indicate) result of %s to %s",
getCharacteristic(),
@@ -1199,17 +1203,17 @@
}
private void startRfcommServer() {
- rfcommServer.setRequestHandler(this::handleRfcommServerRequest);
- rfcommServer.setStateMonitor(state -> {
- logger.log("RfcommServer is in %s state", state);
+ mRfcommServer.setRequestHandler(this::handleRfcommServerRequest);
+ mRfcommServer.setStateMonitor(state -> {
+ mLogger.log("RfcommServer is in %s state", state);
if (CONNECTED.equals(state)) {
sendModelId();
- sendDeviceBleAddress(bleAddress);
+ sendDeviceBleAddress(mBleAddress);
sendFirmwareVersion();
sendSessionNonce();
}
});
- rfcommServer.start();
+ mRfcommServer.start();
}
private void handleRfcommServerRequest(int eventGroup, int eventCode, byte[] data) {
@@ -1221,25 +1225,25 @@
String deviceValue = base16().encode(data);
if (eventCode == DeviceEventCode.DEVICE_CAPABILITY_VALUE) {
- logger.log("Received phone capability: %s", deviceValue);
+ mLogger.log("Received phone capability: %s", deviceValue);
} else if (eventCode == DeviceEventCode.PLATFORM_TYPE_VALUE) {
- logger.log("Received platform type: %s", deviceValue);
+ mLogger.log("Received platform type: %s", deviceValue);
}
break;
case EventGroup.DEVICE_ACTION_VALUE:
if (eventCode == DeviceActionEventCode.DEVICE_ACTION_RING_VALUE) {
- logger.log("receive device action with ring value, data = %d",
+ mLogger.log("receive device action with ring value, data = %d",
data[0]);
sendDeviceRingActionResponse();
// Simulate notifying the seeker that the ringing has stopped due
// to user interaction (such as tapping the bud).
- uiThreadHandler.postDelayed(this::sendDeviceRingStoppedAction,
+ mUiThreadHandler.postDelayed(this::sendDeviceRingStoppedAction,
5000);
}
break;
case EventGroup.DEVICE_CONFIGURATION_VALUE:
if (eventCode == DeviceConfigurationEventCode.CONFIGURATION_BUFFER_SIZE_VALUE) {
- logger.log(
+ mLogger.log(
"receive device action with buffer size value, data = %s",
base16().encode(data));
sendSetBufferActionResponse(data);
@@ -1247,7 +1251,7 @@
break;
case EventGroup.DEVICE_CAPABILITY_SYNC_VALUE:
if (eventCode == DeviceCapabilitySyncEventCode.REQUEST_CAPABILITY_UPDATE_VALUE) {
- logger.log("receive device capability update request.");
+ mLogger.log("receive device capability update request.");
sendCapabilitySync();
}
break;
@@ -1257,23 +1261,23 @@
}
private void stopRfcommServer() {
- rfcommServer.stop();
- rfcommServer.setRequestHandler(null);
- rfcommServer.setStateMonitor(null);
+ mRfcommServer.stop();
+ mRfcommServer.setRequestHandler(null);
+ mRfcommServer.setStateMonitor(null);
}
private void sendModelId() {
- logger.log("Send model ID to the client");
- rfcommServer.send(
+ mLogger.log("Send model ID to the client");
+ mRfcommServer.send(
EventGroup.DEVICE_VALUE,
DeviceEventCode.DEVICE_MODEL_ID_VALUE,
modelIdServiceData(/* forAdvertising= */ false));
}
private void sendDeviceBleAddress(String bleAddress) {
- logger.log("Send BLE address (%s) to the client", bleAddress);
+ mLogger.log("Send BLE address (%s) to the client", bleAddress);
if (bleAddress != null) {
- rfcommServer.send(
+ mRfcommServer.send(
EventGroup.DEVICE_VALUE,
DeviceEventCode.DEVICE_BLE_ADDRESS_VALUE,
BluetoothAddress.decode(bleAddress));
@@ -1281,25 +1285,25 @@
}
private void sendFirmwareVersion() {
- logger.log("Send Firmware Version (%s) to the client", deviceFirmwareVersion);
- rfcommServer.send(
+ mLogger.log("Send Firmware Version (%s) to the client", mDeviceFirmwareVersion);
+ mRfcommServer.send(
EventGroup.DEVICE_VALUE,
DeviceEventCode.FIRMWARE_VERSION_VALUE,
- deviceFirmwareVersion.getBytes());
+ mDeviceFirmwareVersion.getBytes());
}
private void sendSessionNonce() {
- logger.log("Send SessionNonce (%s) to the client", deviceFirmwareVersion);
+ mLogger.log("Send SessionNonce (%s) to the client", mDeviceFirmwareVersion);
SecureRandom secureRandom = new SecureRandom();
- sessionNonce = new byte[SECTION_NONCE_LENGTH];
- secureRandom.nextBytes(sessionNonce);
- rfcommServer.send(
- EventGroup.DEVICE_VALUE, DeviceEventCode.SECTION_NONCE_VALUE, sessionNonce);
+ mSessionNonce = new byte[SECTION_NONCE_LENGTH];
+ secureRandom.nextBytes(mSessionNonce);
+ mRfcommServer.send(
+ EventGroup.DEVICE_VALUE, DeviceEventCode.SECTION_NONCE_VALUE, mSessionNonce);
}
private void sendDeviceRingActionResponse() {
- logger.log("Send device ring action response to the client");
- rfcommServer.send(
+ mLogger.log("Send device ring action response to the client");
+ mRfcommServer.send(
EventGroup.ACKNOWLEDGEMENT_VALUE,
AcknowledgementEventCode.ACKNOWLEDGEMENT_ACK_VALUE,
new byte[]{
@@ -1313,9 +1317,9 @@
for (ByteString accountKey : getAccountKeys()) {
try {
if (MessageStreamHmacEncoder.verifyHmac(
- accountKey.toByteArray(), sessionNonce, data)) {
+ accountKey.toByteArray(), mSessionNonce, data)) {
hmacPassed = true;
- logger.log("Buffer size data matches account key %s",
+ mLogger.log("Buffer size data matches account key %s",
base16().encode(accountKey.toByteArray()));
break;
}
@@ -1324,8 +1328,8 @@
}
}
if (hmacPassed) {
- logger.log("Send buffer size action response %s to the client", base16().encode(data));
- rfcommServer.send(
+ mLogger.log("Send buffer size action response %s to the client", base16().encode(data));
+ mRfcommServer.send(
EventGroup.ACKNOWLEDGEMENT_VALUE,
AcknowledgementEventCode.ACKNOWLEDGEMENT_ACK_VALUE,
new byte[]{
@@ -1336,15 +1340,15 @@
data[2]
});
} else {
- logger.log("No matched account key for sendSetBufferActionResponse");
+ mLogger.log("No matched account key for sendSetBufferActionResponse");
}
}
private void sendCapabilitySync() {
- logger.log("Send capability sync to the client");
- if (supportDynamicBufferSize) {
- logger.log("Send dynamic buffer size range to the client");
- rfcommServer.send(
+ mLogger.log("Send capability sync to the client");
+ if (mSupportDynamicBufferSize) {
+ mLogger.log("Send dynamic buffer size range to the client");
+ mRfcommServer.send(
EventGroup.DEVICE_CAPABILITY_SYNC_VALUE,
DeviceCapabilitySyncEventCode.CONFIGURABLE_BUFFER_SIZE_RANGE_VALUE,
new byte[]{
@@ -1358,8 +1362,8 @@
}
private void sendDeviceRingStoppedAction() {
- logger.log("Sending device ring stopped action to the client");
- rfcommServer.send(
+ mLogger.log("Sending device ring stopped action to the client");
+ mRfcommServer.send(
EventGroup.DEVICE_ACTION_VALUE,
DeviceActionEventCode.DEVICE_ACTION_RING_VALUE,
// Additional data for stopping ringing on all components.
@@ -1379,7 +1383,7 @@
public void write(
BluetoothGattServerConnection connection, int offset, byte[] value)
throws BluetoothGattException {
- logger.log("Requested TDS Control Point write, value=%s",
+ mLogger.log("Requested TDS Control Point write, value=%s",
base16().encode(value));
ResultCode resultCode = checkTdsControlPointRequest(value);
@@ -1387,19 +1391,19 @@
try {
becomeDiscoverable();
} catch (TimeoutException | InterruptedException e) {
- logger.log(e, "Failed to become discoverable");
+ mLogger.log(e, "Failed to become discoverable");
resultCode = ResultCode.OPERATION_FAILED;
}
}
- logger.log("Request complete, resultCode=%s", resultCode);
+ mLogger.log("Request complete, resultCode=%s", resultCode);
- logger.log("Sending TDS Control Point response indication");
+ mLogger.log("Sending TDS Control Point response indication");
sendNotification(
Bytes.concat(
new byte[]{
getTdsControlPointOpCode(value),
- resultCode.byteValue,
+ resultCode.mByteValue,
},
resultCode == ResultCode.SUCCESS
? TDS_CONTROL_POINT_RESPONSE_PARAMETER
@@ -1420,7 +1424,7 @@
public byte[] read(BluetoothGattServerConnection connection, int offset) {
return Bytes.concat(
new byte[]{BrHandoverDataCharacteristic.BR_EDR_FEATURES},
- bluetoothAddress.getBytes(ByteOrder.LITTLE_ENDIAN),
+ mBluetoothAddress.getBytes(ByteOrder.LITTLE_ENDIAN),
CLASS_OF_DEVICE.getBytes(ByteOrder.LITTLE_ENDIAN));
}
};
@@ -1436,7 +1440,7 @@
0 /* no properties */,
0 /* no permissions */);
- if (options.getIncludeTransportDataDescriptor()) {
+ if (mOptions.getIncludeTransportDataDescriptor()) {
characteristic.addDescriptor(
new BluetoothGattDescriptor(
TransportDiscoveryService.BluetoothSigDataCharacteristic
@@ -1471,16 +1475,16 @@
@Override
public void write(
BluetoothGattServerConnection connection, int offset, byte[] value) {
- logger.log("Got value from account key servlet: %s",
+ mLogger.log("Got value from account key servlet: %s",
base16().encode(value));
try {
- addAccountKey(AesEcbSingleBlockEncryption.decrypt(secret, value),
- pairingDevice);
+ addAccountKey(AesEcbSingleBlockEncryption.decrypt(mSecret, value),
+ mPairingDevice);
} catch (GeneralSecurityException e) {
- logger.log(e, "Failed to decrypt account key.");
+ mLogger.log(e, "Failed to decrypt account key.");
}
- uiThreadHandler.post(
- () -> advertiser.startAdvertising(accountKeysServiceData()));
+ mUiThreadHandler.post(
+ () -> mAdvertiser.startAdvertising(accountKeysServiceData()));
}
};
@@ -1494,7 +1498,7 @@
@Override
public byte[] read(BluetoothGattServerConnection connection, int offset) {
- return deviceFirmwareVersion.getBytes();
+ return mDeviceFirmwareVersion.getBytes();
}
};
@@ -1514,10 +1518,10 @@
@Override
public void write(
BluetoothGattServerConnection connection, int offset, byte[] value) {
- logger.log("Requesting key based pairing handshake, value=%s",
+ mLogger.log("Requesting key based pairing handshake, value=%s",
base16().encode(value));
- secret = null;
+ mSecret = null;
byte[] seekerPublicAddress = null;
if (value.length == AES_BLOCK_LENGTH) {
@@ -1525,67 +1529,67 @@
byte[] candidateSecret = key.toByteArray();
try {
seekerPublicAddress = handshake(candidateSecret, value);
- secret = candidateSecret;
- isSubsequentPair = true;
+ mSecret = candidateSecret;
+ mIsSubsequentPair = true;
break;
} catch (GeneralSecurityException e) {
- logger.log(e, "Failed to decrypt with %s",
+ mLogger.log(e, "Failed to decrypt with %s",
base16().encode(candidateSecret));
}
}
} else if (value.length == AES_BLOCK_LENGTH + PUBLIC_KEY_LENGTH
- && options.getAntiSpoofingPrivateKey() != null) {
+ && mOptions.getAntiSpoofingPrivateKey() != null) {
try {
byte[] encryptedRequest = Arrays.copyOf(value, AES_BLOCK_LENGTH);
byte[] receivedPublicKey =
Arrays.copyOfRange(value, AES_BLOCK_LENGTH, value.length);
byte[] candidateSecret =
EllipticCurveDiffieHellmanExchange.create(
- options.getAntiSpoofingPrivateKey())
+ mOptions.getAntiSpoofingPrivateKey())
.generateSecret(receivedPublicKey);
seekerPublicAddress = handshake(candidateSecret, encryptedRequest);
- secret = candidateSecret;
+ mSecret = candidateSecret;
} catch (Exception e) {
- logger.log(
+ mLogger.log(
e,
"Failed to decrypt with anti-spoofing private key %s",
- base16().encode(options.getAntiSpoofingPrivateKey()));
+ base16().encode(mOptions.getAntiSpoofingPrivateKey()));
}
} else {
- logger.log("Packet length invalid, %d", value.length);
+ mLogger.log("Packet length invalid, %d", value.length);
return;
}
- if (secret == null) {
- logger.log("Couldn't find a usable key to decrypt with.");
+ if (mSecret == null) {
+ mLogger.log("Couldn't find a usable key to decrypt with.");
return;
}
- logger.log("Found valid decryption key, %s", base16().encode(secret));
+ mLogger.log("Found valid decryption key, %s", base16().encode(mSecret));
byte[] salt = new byte[9];
new Random().nextBytes(salt);
try {
byte[] data = concat(
new byte[]{KeyBasedPairingCharacteristic.Response.TYPE},
- bluetoothAddress.getBytes(ByteOrder.BIG_ENDIAN), salt);
- byte[] encryptedAddress = encrypt(secret, data);
- logger.log(
+ mBluetoothAddress.getBytes(ByteOrder.BIG_ENDIAN), salt);
+ byte[] encryptedAddress = encrypt(mSecret, data);
+ mLogger.log(
"Sending handshake response %s with size %d",
base16().encode(encryptedAddress), encryptedAddress.length);
sendNotification(encryptedAddress);
// Notify seeker for NameCharacteristic to get provider device name
// when seeker request device name flag is true.
- if (options.getEnableNameCharacteristic()
- && handshakeRequest.requestDeviceName()) {
+ if (mOptions.getEnableNameCharacteristic()
+ && mHandshakeRequest.requestDeviceName()) {
byte[] encryptedResponse =
getDeviceNameInBytes() != null ? createEncryptedDeviceName()
: new byte[0];
- logger.log(
+ mLogger.log(
"Sending device name response %s with size %d",
base16().encode(encryptedResponse),
encryptedResponse.length);
- deviceNameServlet.sendNotification(encryptedResponse);
+ mDeviceNameServlet.sendNotification(encryptedResponse);
}
// Disconnects the current connection to allow the following pairing
@@ -1598,77 +1602,77 @@
// If headphones support multiple simultaneous connections, they
// should stay connected. But Android fails to pair with the new
// device if we don't first disconnect from any other device.
- logger.log("Skip remove bond, value=%s",
- options.getRemoveAllDevicesDuringPairing());
- if (options.getRemoveAllDevicesDuringPairing()
- && handshakeRequest.getType()
+ mLogger.log("Skip remove bond, value=%s",
+ mOptions.getRemoveAllDevicesDuringPairing());
+ if (mOptions.getRemoveAllDevicesDuringPairing()
+ && mHandshakeRequest.getType()
== HandshakeRequest.Type.KEY_BASED_PAIRING_REQUEST
- && !handshakeRequest.requestRetroactivePair()) {
- executor.execute(() -> disconnect());
+ && !mHandshakeRequest.requestRetroactivePair()) {
+ mExecutor.execute(() -> disconnect());
}
- if (handshakeRequest.getType()
+ if (mHandshakeRequest.getType()
== HandshakeRequest.Type.KEY_BASED_PAIRING_REQUEST
- && handshakeRequest.requestProviderInitialBonding()) {
+ && mHandshakeRequest.requestProviderInitialBonding()) {
// Run on executor to ensure it doesn't happen until after the
// notify (which tells the remote device what address to expect).
String seekerPublicAddressString =
BluetoothAddress.encode(seekerPublicAddress);
- executor.execute(() -> {
- logger.log("Sending pairing request to %s",
+ mExecutor.execute(() -> {
+ mLogger.log("Sending pairing request to %s",
seekerPublicAddressString);
- bluetoothAdapter.getRemoteDevice(
+ mBluetoothAdapter.getRemoteDevice(
seekerPublicAddressString).createBond();
});
}
} catch (GeneralSecurityException e) {
- logger.log(e, "Failed to notify of static mac address");
+ mLogger.log(e, "Failed to notify of static mac address");
}
}
@Nullable
private byte[] handshake(byte[] key, byte[] encryptedPairingRequest)
throws GeneralSecurityException {
- handshakeRequest = new HandshakeRequest(key, encryptedPairingRequest);
+ mHandshakeRequest = new HandshakeRequest(key, encryptedPairingRequest);
- byte[] decryptedAddress = handshakeRequest.getVerificationData();
- if (bleAddress != null
+ byte[] decryptedAddress = mHandshakeRequest.getVerificationData();
+ if (mBleAddress != null
&& Arrays.equals(decryptedAddress,
- BluetoothAddress.decode(bleAddress))
+ BluetoothAddress.decode(mBleAddress))
|| Arrays.equals(decryptedAddress,
- bluetoothAddress.getBytes(ByteOrder.BIG_ENDIAN))) {
- logger.log("Address matches: %s", base16().encode(decryptedAddress));
+ mBluetoothAddress.getBytes(ByteOrder.BIG_ENDIAN))) {
+ mLogger.log("Address matches: %s", base16().encode(decryptedAddress));
} else {
throw new GeneralSecurityException(
"Address (BLE or BR/EDR) is not correct: "
+ base16().encode(decryptedAddress)
+ ", "
- + bleAddress
+ + mBleAddress
+ ", "
+ getBluetoothAddress());
}
- switch (handshakeRequest.getType()) {
+ switch (mHandshakeRequest.getType()) {
case KEY_BASED_PAIRING_REQUEST:
- return handleKeyBasedPairingRequest(handshakeRequest);
+ return handleKeyBasedPairingRequest(mHandshakeRequest);
case ACTION_OVER_BLE:
- return handleActionOverBleRequest(handshakeRequest);
+ return handleActionOverBleRequest(mHandshakeRequest);
case UNKNOWN:
// continue to throw the exception;
}
throw new GeneralSecurityException(
- "Type is not correct: " + handshakeRequest.getType());
+ "Type is not correct: " + mHandshakeRequest.getType());
}
@Nullable
private byte[] handleKeyBasedPairingRequest(HandshakeRequest handshakeRequest)
throws GeneralSecurityException {
if (handshakeRequest.requestDiscoverable()) {
- logger.log("Requested discoverability");
+ mLogger.log("Requested discoverability");
setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
}
- logger.log(
+ mLogger.log(
"KeyBasedPairing: initialBonding=%s, requestDeviceName=%s, "
+ "retroactivePair=%s",
handshakeRequest.requestProviderInitialBonding(),
@@ -1679,13 +1683,14 @@
if (handshakeRequest.requestProviderInitialBonding()
|| handshakeRequest.requestRetroactivePair()) {
seekerPublicAddress = handshakeRequest.getSeekerPublicAddress();
- logger.log(
+ mLogger.log(
"Seeker sends BR/EDR address %s to provider",
BluetoothAddress.encode(seekerPublicAddress));
}
if (handshakeRequest.requestRetroactivePair()) {
- if (bluetoothAdapter.getRemoteDevice(seekerPublicAddress).getBondState()
+ if (mBluetoothAdapter.getRemoteDevice(
+ seekerPublicAddress).getBondState()
!= BluetoothDevice.BOND_BONDED) {
throw new GeneralSecurityException(
"Address (BR/EDR) is not bonded: "
@@ -1700,14 +1705,14 @@
private byte[] handleActionOverBleRequest(HandshakeRequest handshakeRequest) {
// TODO(wollohchou): implement action over ble request.
if (handshakeRequest.requestDeviceAction()) {
- logger.log("Requesting action over BLE, device action");
+ mLogger.log("Requesting action over BLE, device action");
} else if (handshakeRequest.requestFollowedByAdditionalData()) {
- logger.log(
+ mLogger.log(
"Requesting action over BLE, followed by additional data, "
+ "type:%s",
handshakeRequest.getAdditionalDataType());
} else {
- logger.log("Requesting action over BLE");
+ mLogger.log("Requesting action over BLE");
}
return null;
}
@@ -1718,14 +1723,14 @@
private byte[] createEncryptedDeviceName() throws GeneralSecurityException {
byte[] deviceName = getDeviceNameInBytes();
String providerName = new String(deviceName, StandardCharsets.UTF_8);
- logger.log(
+ mLogger.log(
"Sending handshake response for device name %s with size %d",
providerName, deviceName.length);
- return NamingEncoder.encodeNamingPacket(secret, providerName);
+ return NamingEncoder.encodeNamingPacket(mSecret, providerName);
}
};
- beaconActionsServlet =
+ mBeaconActionsServlet =
new NotifiableGattServlet() {
private static final int GATT_ERROR_UNAUTHENTICATED = 0x80;
private static final int GATT_ERROR_INVALID_VALUE = 0x81;
@@ -1735,17 +1740,17 @@
private static final int IDENTITY_KEY_LENGTH = 32;
private static final byte TRANSMISSION_POWER = 0;
- private final SecureRandom random = new SecureRandom();
- private final MessageDigest sha256;
+ private final SecureRandom mRandom = new SecureRandom();
+ private final MessageDigest mSha256;
@Nullable
- private byte[] lastNonce;
+ private byte[] mLastNonce;
@Nullable
- private ByteString identityKey = options.getEddystoneIdentityKey();
+ private ByteString mIdentityKey = mOptions.getEddystoneIdentityKey();
{
try {
- sha256 = MessageDigest.getInstance("SHA-256");
- sha256.reset();
+ mSha256 = MessageDigest.getInstance("SHA-256");
+ mSha256.reset();
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(
"System missing SHA-256 implementation.", e);
@@ -1764,19 +1769,19 @@
@Override
public byte[] read(BluetoothGattServerConnection connection, int offset) {
- lastNonce = new byte[NONCE_LENGTH];
- random.nextBytes(lastNonce);
- return lastNonce;
+ mLastNonce = new byte[NONCE_LENGTH];
+ mRandom.nextBytes(mLastNonce);
+ return mLastNonce;
}
@Override
public void write(
BluetoothGattServerConnection connection, int offset, byte[] value)
throws BluetoothGattException {
- logger.log("Got value from beacon actions servlet: %s",
+ mLogger.log("Got value from beacon actions servlet: %s",
base16().encode(value));
if (value.length == 0) {
- logger.log("Packet length invalid, %d", value.length);
+ mLogger.log("Packet length invalid, %d", value.length);
throw new BluetoothGattException("Packet length invalid",
GATT_ERROR_INVALID_VALUE);
}
@@ -1807,7 +1812,7 @@
private boolean verifyAccountKeyToken(byte[] value, boolean ownerOnly)
throws BluetoothGattException {
if (value.length < ONE_TIME_AUTH_KEY_LENGTH + ONE_TIME_AUTH_KEY_OFFSET) {
- logger.log("Packet length invalid, %d", value.length);
+ mLogger.log("Packet length invalid, %d", value.length);
throw new BluetoothGattException(
"Packet length invalid", GATT_ERROR_INVALID_VALUE);
}
@@ -1816,27 +1821,28 @@
value,
ONE_TIME_AUTH_KEY_OFFSET,
ONE_TIME_AUTH_KEY_LENGTH + ONE_TIME_AUTH_KEY_OFFSET);
- if (lastNonce == null) {
+ if (mLastNonce == null) {
throw new BluetoothGattException(
"Nonce wasn't set", GATT_ERROR_UNAUTHENTICATED);
}
if (ownerOnly) {
ByteString accountKey = getOwnerAccountKey();
if (accountKey != null) {
- sha256.update(accountKey.toByteArray());
- sha256.update(lastNonce);
+ mSha256.update(accountKey.toByteArray());
+ mSha256.update(mLastNonce);
return Arrays.equals(
hashedAccountKey,
- Arrays.copyOf(sha256.digest(), ONE_TIME_AUTH_KEY_LENGTH));
+ Arrays.copyOf(mSha256.digest(), ONE_TIME_AUTH_KEY_LENGTH));
}
} else {
Set<ByteString> accountKeys = getAccountKeys();
for (ByteString accountKey : accountKeys) {
- sha256.update(accountKey.toByteArray());
- sha256.update(lastNonce);
+ mSha256.update(accountKey.toByteArray());
+ mSha256.update(mLastNonce);
if (Arrays.equals(
hashedAccountKey,
- Arrays.copyOf(sha256.digest(), ONE_TIME_AUTH_KEY_LENGTH))) {
+ Arrays.copyOf(mSha256.digest(),
+ ONE_TIME_AUTH_KEY_LENGTH))) {
return true;
}
}
@@ -1889,7 +1895,7 @@
if (verifyAccountKeyToken(value, /* ownerOnly= */ true)) {
flags |= (byte) (1 << 1);
}
- if (identityKey == null) {
+ if (mIdentityKey == null) {
sendNotification(
fromBytes(
(byte) BeaconActionType.READ_PROVISIONING_STATE,
@@ -1905,7 +1911,7 @@
flags)
.concat(
E2eeCalculator.computeE2eeEid(
- identityKey, /* exponent= */ 10,
+ mIdentityKey, /* exponent= */ 10,
getBeaconClock()))
.toByteArray());
}
@@ -1921,17 +1927,17 @@
if (value.length
!= ONE_TIME_AUTH_KEY_LENGTH + ONE_TIME_AUTH_KEY_OFFSET
+ IDENTITY_KEY_LENGTH) {
- logger.log("Packet length invalid, %d", value.length);
+ mLogger.log("Packet length invalid, %d", value.length);
throw new BluetoothGattException("Packet length invalid",
GATT_ERROR_INVALID_VALUE);
}
- if (identityKey != null) {
+ if (mIdentityKey != null) {
throw new BluetoothGattException(
"Device is already provisioned as Eddystone",
GATT_ERROR_UNAUTHENTICATED);
}
- identityKey = Crypto.aesEcbNoPaddingDecrypt(
- ByteString.copyFrom(ownerAccountKey),
+ mIdentityKey = Crypto.aesEcbNoPaddingDecrypt(
+ ByteString.copyFrom(mOwnerAccountKey),
ByteString.copyFrom(value)
.substring(ONE_TIME_AUTH_KEY_LENGTH
+ ONE_TIME_AUTH_KEY_OFFSET));
@@ -1942,10 +1948,10 @@
new ServiceConfig()
.addCharacteristic(accountKeyServlet)
.addCharacteristic(keyBasedPairingServlet)
- .addCharacteristic(passkeyServlet)
+ .addCharacteristic(mPasskeyServlet)
.addCharacteristic(firmwareVersionServlet);
- if (options.getEnableBeaconActionsCharacteristic()) {
- fastPairServiceConfig.addCharacteristic(beaconActionsServlet);
+ if (mOptions.getEnableBeaconActionsCharacteristic()) {
+ fastPairServiceConfig.addCharacteristic(mBeaconActionsServlet);
}
BluetoothGattServerConfig config =
@@ -1958,17 +1964,18 @@
.addCharacteristic(bluetoothSigServlet))
.addService(
FastPairService.ID,
- options.getEnableNameCharacteristic()
- ? fastPairServiceConfig.addCharacteristic(deviceNameServlet)
+ mOptions.getEnableNameCharacteristic()
+ ? fastPairServiceConfig.addCharacteristic(
+ mDeviceNameServlet)
: fastPairServiceConfig);
- logger.log(
+ mLogger.log(
"Starting GATT server, support name characteristic %b",
- options.getEnableNameCharacteristic());
+ mOptions.getEnableNameCharacteristic());
try {
helper.open(config);
} catch (BluetoothException e) {
- logger.log(e, "Error starting GATT server");
+ mLogger.log(e, "Error starting GATT server");
}
}
@@ -1978,36 +1985,37 @@
}
public void enterPassKey(int passkey) {
- logger.log("enterPassKey called with passkey %d.", passkey);
+ mLogger.log("enterPassKey called with passkey %d.", passkey);
try {
boolean result =
- (Boolean) Reflect.on(pairingDevice).withMethod("setPasskey", int.class).get(
+ (Boolean) Reflect.on(mPairingDevice).withMethod("setPasskey", int.class).get(
passkey);
- logger.log("enterPassKey called with result %b", result);
+ mLogger.log("enterPassKey called with result %b", result);
} catch (ReflectionException e) {
- logger.log("enterPassKey meet Exception %s.", e.getMessage());
+ mLogger.log("enterPassKey meet Exception %s.", e.getMessage());
}
}
private void checkPasskey() {
// There's a race between the PAIRING_REQUEST broadcast from the OS giving us the local
// passkey, and the remote passkey received over GATT. Skip the check until we have both.
- if (localPasskey == 0 || remotePasskey == 0) {
- logger.log(
+ if (mLocalPasskey == 0 || mRemotePasskey == 0) {
+ mLogger.log(
"Skipping passkey check, missing local (%s) or remote (%s).",
- localPasskey, remotePasskey);
+ mLocalPasskey, mRemotePasskey);
return;
}
// Regardless of whether it matches, send our (encrypted) passkey to the seeker.
- sendPasskeyToRemoteDevice(localPasskey);
+ sendPasskeyToRemoteDevice(mLocalPasskey);
- logger.log("Checking localPasskey %s == remotePasskey %s", localPasskey, remotePasskey);
- boolean passkeysMatched = localPasskey == remotePasskey;
- if (options.getShowsPasskeyConfirmation() && passkeysMatched
- && passkeyEventCallback != null) {
- logger.log("callbacks the UI for passkey confirmation.");
- passkeyEventCallback.onPasskeyConfirmation(localPasskey, this::setPasskeyConfirmation);
+ mLogger.log("Checking localPasskey %s == remotePasskey %s", mLocalPasskey, mRemotePasskey);
+ boolean passkeysMatched = mLocalPasskey == mRemotePasskey;
+ if (mOptions.getShowsPasskeyConfirmation() && passkeysMatched
+ && mPasskeyEventCallback != null) {
+ mLogger.log("callbacks the UI for passkey confirmation.");
+ mPasskeyEventCallback.onPasskeyConfirmation(mLocalPasskey,
+ this::setPasskeyConfirmation);
} else {
setPasskeyConfirmation(passkeysMatched);
}
@@ -2015,45 +2023,45 @@
private void sendPasskeyToRemoteDevice(int passkey) {
try {
- passkeyServlet.sendNotification(
+ mPasskeyServlet.sendNotification(
PasskeyCharacteristic.encrypt(
- PasskeyCharacteristic.Type.PROVIDER, secret, passkey));
+ PasskeyCharacteristic.Type.PROVIDER, mSecret, passkey));
} catch (GeneralSecurityException e) {
- logger.log(e, "Failed to encrypt passkey response.");
+ mLogger.log(e, "Failed to encrypt passkey response.");
}
}
public void setFirmwareVersion(String versionNumber) {
- deviceFirmwareVersion = versionNumber;
+ mDeviceFirmwareVersion = versionNumber;
}
public void setDynamicBufferSize(boolean support) {
- if (supportDynamicBufferSize != support) {
- supportDynamicBufferSize = support;
+ if (mSupportDynamicBufferSize != support) {
+ mSupportDynamicBufferSize = support;
sendCapabilitySync();
}
}
@VisibleForTesting
void setPasskeyConfirmationCallback(PasskeyConfirmationCallback callback) {
- this.passkeyConfirmationCallback = callback;
+ this.mPasskeyConfirmationCallback = callback;
}
public void setDeviceNameCallback(DeviceNameCallback callback) {
- this.deviceNameCallback = callback;
+ this.mDeviceNameCallback = callback;
}
public void setPasskeyEventCallback(PasskeyEventCallback passkeyEventCallback) {
- this.passkeyEventCallback = passkeyEventCallback;
+ this.mPasskeyEventCallback = passkeyEventCallback;
}
private void setPasskeyConfirmation(boolean confirm) {
- pairingDevice.setPairingConfirmation(confirm);
- if (passkeyConfirmationCallback != null) {
- passkeyConfirmationCallback.onPasskeyConfirmation(confirm);
+ mPairingDevice.setPairingConfirmation(confirm);
+ if (mPasskeyConfirmationCallback != null) {
+ mPasskeyConfirmationCallback.onPasskeyConfirmation(confirm);
}
- localPasskey = 0;
- remotePasskey = 0;
+ mLocalPasskey = 0;
+ mRemotePasskey = 0;
}
private void becomeDiscoverable() throws InterruptedException, TimeoutException {
@@ -2066,39 +2074,39 @@
private void setDiscoverable(boolean discoverable)
throws InterruptedException, TimeoutException {
- isDiscoverableLatch = new CountDownLatch(1);
+ mIsDiscoverableLatch = new CountDownLatch(1);
setScanMode(discoverable ? SCAN_MODE_CONNECTABLE_DISCOVERABLE : SCAN_MODE_CONNECTABLE);
// If we're already discoverable, count down the latch right away. Otherwise,
// we'll get a broadcast when we successfully become discoverable.
if (isDiscoverable()) {
- isDiscoverableLatch.countDown();
+ mIsDiscoverableLatch.countDown();
}
- if (isDiscoverableLatch.await(3, TimeUnit.SECONDS)) {
- logger.log("Successfully became switched discoverable mode %s", discoverable);
+ if (mIsDiscoverableLatch.await(3, TimeUnit.SECONDS)) {
+ mLogger.log("Successfully became switched discoverable mode %s", discoverable);
} else {
throw new TimeoutException();
}
}
private void setScanMode(int scanMode) {
- if (revertDiscoverableFuture != null) {
- revertDiscoverableFuture.cancel(false /* may interrupt if running */);
+ if (mRevertDiscoverableFuture != null) {
+ mRevertDiscoverableFuture.cancel(false /* may interrupt if running */);
}
- logger.log("Setting scan mode to %s", scanModeToString(scanMode));
+ mLogger.log("Setting scan mode to %s", scanModeToString(scanMode));
try {
- Method method = bluetoothAdapter.getClass().getMethod("setScanMode", Integer.TYPE);
- method.invoke(bluetoothAdapter, scanMode);
+ Method method = mBluetoothAdapter.getClass().getMethod("setScanMode", Integer.TYPE);
+ method.invoke(mBluetoothAdapter, scanMode);
if (scanMode == SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
- revertDiscoverableFuture =
- executor.schedule(
+ mRevertDiscoverableFuture =
+ mExecutor.schedule(
() -> setScanMode(SCAN_MODE_CONNECTABLE),
- options.getIsMemoryTest() ? 300 : 30,
+ mOptions.getIsMemoryTest() ? 300 : 30,
TimeUnit.SECONDS);
}
} catch (Exception e) {
- logger.log(e, "Error setting scan mode to %d", scanMode);
+ mLogger.log(e, "Error setting scan mode to %d", scanMode);
}
}
@@ -2117,21 +2125,21 @@
private ResultCode checkTdsControlPointRequest(byte[] request) {
if (request.length < 2) {
- logger.log(
+ mLogger.log(
new IllegalArgumentException(), "Expected length >= 2 for %s",
base16().encode(request));
return ResultCode.INVALID_PARAMETER;
}
byte opCode = getTdsControlPointOpCode(request);
if (opCode != ControlPointCharacteristic.ACTIVATE_TRANSPORT_OP_CODE) {
- logger.log(
+ mLogger.log(
new IllegalArgumentException(),
"Expected Activate Transport op code (0x01), got %d",
opCode);
return ResultCode.OP_CODE_NOT_SUPPORTED;
}
if (request[1] != BLUETOOTH_SIG_ORGANIZATION_ID) {
- logger.log(
+ mLogger.log(
new IllegalArgumentException(),
"Expected Bluetooth SIG organization ID (0x01), got %d",
request[1]);
@@ -2145,15 +2153,15 @@
}
private boolean isDiscoverable() {
- return bluetoothAdapter.getScanMode() == SCAN_MODE_CONNECTABLE_DISCOVERABLE;
+ return mBluetoothAdapter.getScanMode() == SCAN_MODE_CONNECTABLE_DISCOVERABLE;
}
private byte[] modelIdServiceData(boolean forAdvertising) {
// Note: This used to be little-endian but is now big-endian. See b/78229467 for details.
byte[] modelIdPacket =
base16().decode(
- forAdvertising ? options.getAdvertisingModelId() : options.getModelId());
- if (!batteryValues.isEmpty()) {
+ forAdvertising ? mOptions.getAdvertisingModelId() : mOptions.getModelId());
+ if (!mBatteryValues.isEmpty()) {
// If we are going to advertise battery values with the packet, then switch to the
// non-3-byte model ID format.
modelIdPacket = concat(new byte[]{0b00000110}, modelIdPacket);
@@ -2187,22 +2195,22 @@
new BloomFilter(
new byte[(int) (1.2 * accountKeys.size()) + 3],
new FastPairBloomFilterHasher());
- String address = bleAddress == null ? SIMULATOR_FAKE_BLE_ADDRESS : bleAddress;
+ String address = mBleAddress == null ? SIMULATOR_FAKE_BLE_ADDRESS : mBleAddress;
// Simulator supports Central Address Resolution characteristic, so when paired, the BLE
// address in Seeker will be resolved to BR/EDR address. This caused Seeker fails on
// checking the bloom filter due to different address is used for salting. In order to
// let battery values notification be shown on paired device, we use random salt to
// workaround it.
- boolean advertisingBatteryValues = !batteryValues.isEmpty();
+ boolean advertisingBatteryValues = !mBatteryValues.isEmpty();
byte[] salt;
- if (options.getUseRandomSaltForAccountKeyRotation() || advertisingBatteryValues) {
+ if (mOptions.getUseRandomSaltForAccountKeyRotation() || advertisingBatteryValues) {
salt = new byte[1];
new SecureRandom().nextBytes(salt);
- logger.log("Using random salt %s for bloom filter", base16().encode(salt));
+ mLogger.log("Using random salt %s for bloom filter", base16().encode(salt));
} else {
salt = BluetoothAddress.decode(address);
- logger.log("Using address %s for bloom filter", address);
+ mLogger.log("Using address %s for bloom filter", address);
}
// To prevent tampering, account filter shall be slightly modified to include battery data
@@ -2219,7 +2227,7 @@
bloomFilter.add(concat(accountKey.toByteArray(), saltAndBatteryData));
}
byte[] packet = generateAccountKeyData(bloomFilter);
- return options.getUseRandomSaltForAccountKeyRotation() || advertisingBatteryValues
+ return mOptions.getUseRandomSaltForAccountKeyRotation() || advertisingBatteryValues
// Create a header with length 1 and type 1 for a random salt.
? concat(packet, createField((byte) 0x11, salt))
// Exclude the salt from the packet, BLE address will be assumed by the client.
@@ -2237,7 +2245,7 @@
}
public int getTxPower() {
- return options.getTxPowerLevel();
+ return mOptions.getTxPowerLevel();
}
@Nullable
@@ -2251,7 +2259,7 @@
@Nullable
private byte[] addBatteryValues(byte[] packet) {
- if (batteryValues.isEmpty() || packet == null) {
+ if (mBatteryValues.isEmpty() || packet == null) {
return packet;
}
@@ -2263,16 +2271,16 @@
// 4 are the type.
// Byte 1 - length: Battery values, the first bit is charging status, the remaining bits are
// the actual value between 0 and 100, or -1 for unknown.
- byte[] batteryData = new byte[batteryValues.size() + 1];
- batteryData[0] = (byte) (batteryValues.size() << 4
- | (suppressBatteryNotification ? 0b0100 : 0b0011));
+ byte[] batteryData = new byte[mBatteryValues.size() + 1];
+ batteryData[0] = (byte) (mBatteryValues.size() << 4
+ | (mSuppressBatteryNotification ? 0b0100 : 0b0011));
int batteryValueIndex = 1;
- for (BatteryValue batteryValue : batteryValues) {
+ for (BatteryValue batteryValue : mBatteryValues) {
batteryData[batteryValueIndex++] =
(byte)
- ((batteryValue.charging ? 0b10000000 : 0b00000000)
- | (0b01111111 & batteryValue.level));
+ ((batteryValue.mCharging ? 0b10000000 : 0b00000000)
+ | (0b01111111 & batteryValue.mLevel));
}
return batteryData;
@@ -2284,16 +2292,16 @@
// The following bytes are the data of bloom filter.
byte[] filterBytes = bloomFilter.asBytes();
byte lengthAndType = (byte) (filterBytes.length << 4
- | (suppressSubsequentPairingNotification ? 0b0010 : 0b0000));
- logger.log(
+ | (mSuppressSubsequentPairingNotification ? 0b0010 : 0b0000));
+ mLogger.log(
"Generate bloom filter with suppress subsequent pairing notification:%b",
- suppressSubsequentPairingNotification);
+ mSuppressSubsequentPairingNotification);
return createField(lengthAndType, filterBytes);
}
/** Disconnects all connected devices. */
private void disconnect() {
- for (BluetoothDevice device : bluetoothAdapter.getBondedDevices()) {
+ for (BluetoothDevice device : mBluetoothAdapter.getBondedDevices()) {
if (device.getBluetoothClass().getMajorDeviceClass() == Major.PHONE) {
removeBond(device);
}
@@ -2304,7 +2312,7 @@
try {
Reflect.on(profile).withMethod("disconnect", BluetoothDevice.class).invoke(device);
} catch (ReflectionException e) {
- logger.log(e, "Error disconnecting device=%s from profile=%s", device, profile);
+ mLogger.log(e, "Error disconnecting device=%s from profile=%s", device, profile);
}
}
@@ -2312,16 +2320,16 @@
try {
Reflect.on(device).withMethod("removeBond").invoke();
} catch (ReflectionException e) {
- logger.log(e, "Error removing bond for device=%s", device);
+ mLogger.log(e, "Error removing bond for device=%s", device);
}
}
public void resetAccountKeys() {
- fastPairSimulatorDatabase.setAccountKeys(new HashSet<>());
- fastPairSimulatorDatabase.setFastPairSeekerDevices(new HashSet<>());
- accountKey = null;
- ownerAccountKey = null;
- logger.log("Remove all account keys");
+ mFastPairSimulatorDatabase.setAccountKeys(new HashSet<>());
+ mFastPairSimulatorDatabase.setFastPairSeekerDevices(new HashSet<>());
+ mAccountKey = null;
+ mOwnerAccountKey = null;
+ mLogger.log("Remove all account keys");
}
public void addAccountKey(byte[] key) {
@@ -2329,43 +2337,43 @@
}
private void addAccountKey(byte[] key, @Nullable BluetoothDevice device) {
- accountKey = key;
- if (ownerAccountKey == null) {
- ownerAccountKey = key;
+ mAccountKey = key;
+ if (mOwnerAccountKey == null) {
+ mOwnerAccountKey = key;
}
- fastPairSimulatorDatabase.addAccountKey(key);
- fastPairSimulatorDatabase.addFastPairSeekerDevice(device, key);
- logger.log("Add account key: key=%s, device=%s", base16().encode(key), device);
+ mFastPairSimulatorDatabase.addAccountKey(key);
+ mFastPairSimulatorDatabase.addFastPairSeekerDevice(device, key);
+ mLogger.log("Add account key: key=%s, device=%s", base16().encode(key), device);
}
private Set<ByteString> getAccountKeys() {
- return fastPairSimulatorDatabase.getAccountKeys();
+ return mFastPairSimulatorDatabase.getAccountKeys();
}
/** Get the latest account key. */
@Nullable
public ByteString getAccountKey() {
- if (accountKey == null) {
+ if (mAccountKey == null) {
return null;
}
- return ByteString.copyFrom(accountKey);
+ return ByteString.copyFrom(mAccountKey);
}
/** Get the owner account key (the first account key registered). */
@Nullable
public ByteString getOwnerAccountKey() {
- if (ownerAccountKey == null) {
+ if (mOwnerAccountKey == null) {
return null;
}
- return ByteString.copyFrom(ownerAccountKey);
+ return ByteString.copyFrom(mOwnerAccountKey);
}
public void resetDeviceName() {
- fastPairSimulatorDatabase.setLocalDeviceName(null);
+ mFastPairSimulatorDatabase.setLocalDeviceName(null);
// Trigger simulator to update device name text view.
- if (deviceNameCallback != null) {
- deviceNameCallback.onNameChanged(getDeviceName());
+ if (mDeviceNameCallback != null) {
+ mDeviceNameCallback.onNameChanged(getDeviceName());
}
}
@@ -2375,18 +2383,18 @@
}
private void setDeviceName(@Nullable byte[] deviceName) {
- fastPairSimulatorDatabase.setLocalDeviceName(deviceName);
+ mFastPairSimulatorDatabase.setLocalDeviceName(deviceName);
- logger.log("Save device name : %s", getDeviceName());
+ mLogger.log("Save device name : %s", getDeviceName());
// Trigger simulator to update device name text view.
- if (deviceNameCallback != null) {
- deviceNameCallback.onNameChanged(getDeviceName());
+ if (mDeviceNameCallback != null) {
+ mDeviceNameCallback.onNameChanged(getDeviceName());
}
}
@Nullable
private byte[] getDeviceNameInBytes() {
- return fastPairSimulatorDatabase.getLocalDeviceName();
+ return mFastPairSimulatorDatabase.getLocalDeviceName();
}
@Nullable
@@ -2395,7 +2403,7 @@
getDeviceNameInBytes() != null
? new String(getDeviceNameInBytes(), StandardCharsets.UTF_8)
: null;
- logger.log("get device name = %s", providerDeviceName);
+ mLogger.log("get device name = %s", providerDeviceName);
return providerDeviceName;
}
@@ -2410,19 +2418,19 @@
* </ul>
*/
private static byte tdsFlags(TransportState transportState) {
- return (byte) (0b00000010 & (transportState.byteValue << 3));
+ return (byte) (0b00000010 & (transportState.mByteValue << 3));
}
/** Detailed information about battery value. */
public static class BatteryValue {
- boolean charging;
+ boolean mCharging;
// The range is 0 ~ 100, and -1 represents the battery level is unknown.
- int level;
+ int mLevel;
public BatteryValue(boolean charging, int level) {
- this.charging = charging;
- this.level = level;
+ this.mCharging = charging;
+ this.mLevel = level;
}
}
}
diff --git a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/FastPairSimulatorDatabase.java b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/FastPairSimulatorDatabase.java
similarity index 84%
rename from nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/FastPairSimulatorDatabase.java
rename to nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/FastPairSimulatorDatabase.java
index 4d2a9e8..254ec51 100644
--- a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/FastPairSimulatorDatabase.java
+++ b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/FastPairSimulatorDatabase.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package com.android.server.nearby.common.bluetooth.fastpair.testing;
+package android.nearby.fastpair.provider;
import static com.google.common.io.BaseEncoding.base16;
@@ -45,15 +45,15 @@
// [for SASS]
private static final String KEY_FAST_PAIR_SEEKER_DEVICE = "FAST_PAIR_SEEKER_DEVICE";
- private final SharedPreferences sharedPreferences;
+ private final SharedPreferences mSharedPreferences;
public FastPairSimulatorDatabase(Context context) {
- sharedPreferences = context.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
+ mSharedPreferences = context.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
}
/** Adds single account key. */
public void addAccountKey(byte[] accountKey) {
- if (sharedPreferences == null) {
+ if (mSharedPreferences == null) {
return;
}
@@ -78,7 +78,7 @@
/** Sets account keys, overrides all. */
public void setAccountKeys(Set<ByteString> accountKeys) {
- if (sharedPreferences == null) {
+ if (mSharedPreferences == null) {
return;
}
@@ -87,16 +87,16 @@
keys.add(base16().encode(item.toByteArray()));
}
- sharedPreferences.edit().putStringSet(KEY_ACCOUNT_KEYS, keys).apply();
+ mSharedPreferences.edit().putStringSet(KEY_ACCOUNT_KEYS, keys).apply();
}
/** Gets all account keys. */
public Set<ByteString> getAccountKeys() {
- if (sharedPreferences == null) {
+ if (mSharedPreferences == null) {
return new HashSet<>();
}
- Set<String> keys = sharedPreferences.getStringSet(KEY_ACCOUNT_KEYS, new HashSet<>());
+ Set<String> keys = mSharedPreferences.getStringSet(KEY_ACCOUNT_KEYS, new HashSet<>());
Set<ByteString> accountKeys = new HashSet<>();
// Add new account keys one by one.
for (String key : keys) {
@@ -108,26 +108,26 @@
/** Sets local device name. */
public void setLocalDeviceName(byte[] deviceName) {
- if (sharedPreferences == null) {
+ if (mSharedPreferences == null) {
return;
}
String humanReadableName = deviceName != null ? new String(deviceName, UTF_8) : null;
if (humanReadableName == null) {
- sharedPreferences.edit().remove(KEY_DEVICE_NAME).apply();
+ mSharedPreferences.edit().remove(KEY_DEVICE_NAME).apply();
} else {
- sharedPreferences.edit().putString(KEY_DEVICE_NAME, humanReadableName).apply();
+ mSharedPreferences.edit().putString(KEY_DEVICE_NAME, humanReadableName).apply();
}
}
/** Gets local device name. */
@Nullable
public byte[] getLocalDeviceName() {
- if (sharedPreferences == null) {
+ if (mSharedPreferences == null) {
return null;
}
- String deviceName = sharedPreferences.getString(KEY_DEVICE_NAME, null);
+ String deviceName = mSharedPreferences.getString(KEY_DEVICE_NAME, null);
return deviceName != null ? deviceName.getBytes(UTF_8) : null;
}
@@ -136,7 +136,7 @@
* href="http://go/smart-audio-source-switching-design">Sass design doc</a>
*/
public void addFastPairSeekerDevice(@Nullable BluetoothDevice device, byte[] accountKey) {
- if (sharedPreferences == null) {
+ if (mSharedPreferences == null) {
return;
}
@@ -164,7 +164,7 @@
/** [for SASS] Sets all seeker device info, overrides all. */
public void setFastPairSeekerDevices(Set<FastPairSeekerDevice> fastPairSeekerDeviceSet) {
- if (sharedPreferences == null) {
+ if (mSharedPreferences == null) {
return;
}
@@ -173,18 +173,18 @@
rawStringSet.add(item.toRawString());
}
- sharedPreferences.edit().putStringSet(KEY_FAST_PAIR_SEEKER_DEVICE, rawStringSet).apply();
+ mSharedPreferences.edit().putStringSet(KEY_FAST_PAIR_SEEKER_DEVICE, rawStringSet).apply();
}
/** [for SASS] Gets all seeker device info. */
public Set<FastPairSeekerDevice> getFastPairSeekerDevices() {
- if (sharedPreferences == null) {
+ if (mSharedPreferences == null) {
return new HashSet<>();
}
Set<FastPairSeekerDevice> fastPairSeekerDevices = new HashSet<>();
Set<String> rawStringSet =
- sharedPreferences.getStringSet(KEY_FAST_PAIR_SEEKER_DEVICE, new HashSet<>());
+ mSharedPreferences.getStringSet(KEY_FAST_PAIR_SEEKER_DEVICE, new HashSet<>());
for (String rawString : rawStringSet) {
FastPairSeekerDevice fastPairDevice = FastPairSeekerDevice.fromRawString(rawString);
if (fastPairDevice == null) {
@@ -201,24 +201,24 @@
private static final int INDEX_DEVICE = 0;
private static final int INDEX_ACCOUNT_KEY = 1;
- private final BluetoothDevice device;
- private final byte[] accountKey;
+ private final BluetoothDevice mDevice;
+ private final byte[] mAccountKey;
private FastPairSeekerDevice(BluetoothDevice device, byte[] accountKey) {
- this.device = device;
- this.accountKey = accountKey;
+ this.mDevice = device;
+ this.mAccountKey = accountKey;
}
public BluetoothDevice getBluetoothDevice() {
- return device;
+ return mDevice;
}
public byte[] getAccountKey() {
- return accountKey;
+ return mAccountKey;
}
public String toRawString() {
- return String.format("%s,%s", device, base16().encode(accountKey));
+ return String.format("%s,%s", mDevice, base16().encode(mAccountKey));
}
/** Decodes the raw string if possible. */
diff --git a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/HandshakeRequest.java b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/HandshakeRequest.java
similarity index 84%
rename from nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/HandshakeRequest.java
rename to nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/HandshakeRequest.java
index 5453a87..9cfffd8 100644
--- a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/HandshakeRequest.java
+++ b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/HandshakeRequest.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package com.android.server.nearby.common.bluetooth.fastpair.testing;
+package android.nearby.fastpair.provider;
import static com.android.server.nearby.common.bluetooth.fastpair.AesEcbSingleBlockEncryption.decrypt;
import static com.android.server.nearby.common.bluetooth.fastpair.Constants.BLUETOOTH_ADDRESS_LENGTH;
@@ -45,7 +45,7 @@
*
* @see {go/fast-pair-spec-handshake-message1}
*/
- private final byte[] decryptedMessage;
+ private final byte[] mDecryptedMessage;
/** Enumerates the handshake message types. */
public enum Type {
@@ -53,14 +53,14 @@
ACTION_OVER_BLE(Request.TYPE_ACTION_OVER_BLE),
UNKNOWN((byte) 0xFF);
- private final byte value;
+ private final byte mValue;
Type(byte type) {
- value = type;
+ mValue = type;
}
public byte getValue() {
- return value;
+ return mValue;
}
public static Type valueOf(byte value) {
@@ -75,21 +75,24 @@
public HandshakeRequest(byte[] key, byte[] encryptedPairingRequest)
throws GeneralSecurityException {
- decryptedMessage = decrypt(key, encryptedPairingRequest);
+ mDecryptedMessage = decrypt(key, encryptedPairingRequest);
}
/**
- * Gets the type of this handshake request. Currently, we have 2 types: 0x00 for Key-based Pairing
- * Request and 0x10 for Action Request.
+ * Gets the type of this handshake request. Currently, we have 2 types: 0x00 for Key-based
+ * Pairing Request and 0x10 for Action Request.
*/
public Type getType() {
- return Type.valueOf(decryptedMessage[Request.TYPE_INDEX]);
+ return Type.valueOf(mDecryptedMessage[Request.TYPE_INDEX]);
}
- /** Gets verification data of this handshake request, currently, we use Provider's BLE address. */
+ /**
+ * Gets verification data of this handshake request.
+ * Currently, we use Provider's BLE address.
+ */
public byte[] getVerificationData() {
return Arrays.copyOfRange(
- decryptedMessage,
+ mDecryptedMessage,
Request.VERIFICATION_DATA_INDEX,
Request.VERIFICATION_DATA_INDEX + Request.VERIFICATION_DATA_LENGTH);
}
@@ -97,7 +100,7 @@
/** Gets Seeker's public address of the handshake request. */
public byte[] getSeekerPublicAddress() {
return Arrays.copyOfRange(
- decryptedMessage,
+ mDecryptedMessage,
Request.SEEKER_PUBLIC_ADDRESS_INDEX,
Request.SEEKER_PUBLIC_ADDRESS_INDEX + BLUETOOTH_ADDRESS_LENGTH);
}
@@ -126,7 +129,7 @@
/** Gets the flags of this handshake request. */
private byte getFlags() {
- return decryptedMessage[Request.FLAGS_INDEX];
+ return mDecryptedMessage[Request.FLAGS_INDEX];
}
/** Checks whether the Seeker requests a device action. */
@@ -134,17 +137,21 @@
return (getFlags() & DEVICE_ACTION) != 0;
}
- /** Checks whether the Seeker requests an action which will be followed by an additional data. */
+ /**
+ * Checks whether the Seeker requests an action which will be followed by an additional data
+ * .
+ */
public boolean requestFollowedByAdditionalData() {
return (getFlags() & ADDITIONAL_DATA_CHARACTERISTIC) != 0;
}
/** Gets the {@link AdditionalDataType} of this handshake request. */
- public @AdditionalDataType int getAdditionalDataType() {
+ @AdditionalDataType
+ public int getAdditionalDataType() {
if (!requestFollowedByAdditionalData()
- || decryptedMessage.length <= ADDITIONAL_DATA_TYPE_INDEX) {
+ || mDecryptedMessage.length <= ADDITIONAL_DATA_TYPE_INDEX) {
return AdditionalDataType.UNKNOWN;
}
- return decryptedMessage[ADDITIONAL_DATA_TYPE_INDEX];
+ return mDecryptedMessage[ADDITIONAL_DATA_TYPE_INDEX];
}
}
diff --git a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/OreoFastPairAdvertiser.java b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/OreoFastPairAdvertiser.java
similarity index 73%
rename from nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/OreoFastPairAdvertiser.java
rename to nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/OreoFastPairAdvertiser.java
index 6913356..dd664ea 100644
--- a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/OreoFastPairAdvertiser.java
+++ b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/OreoFastPairAdvertiser.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package com.android.server.nearby.common.bluetooth.fastpair.testing;
+package android.nearby.fastpair.provider;
import static com.google.common.io.BaseEncoding.base16;
@@ -26,6 +26,7 @@
import android.bluetooth.le.AdvertisingSetCallback;
import android.bluetooth.le.AdvertisingSetParameters;
import android.bluetooth.le.BluetoothLeAdvertiser;
+import android.nearby.fastpair.provider.utils.Logger;
import android.os.Build.VERSION_CODES;
import android.os.ParcelUuid;
@@ -43,41 +44,43 @@
@TargetApi(VERSION_CODES.O)
public final class OreoFastPairAdvertiser implements FastPairAdvertiser {
private static final String TAG = "OreoFastPairAdvertiser";
- private final Logger logger = new Logger(TAG);
+ private final Logger mLogger = new Logger(TAG);
- private final FastPairSimulator simulator;
- private final BluetoothLeAdvertiser advertiser;
- private final AdvertisingSetCallback advertisingSetCallback;
- private AdvertisingSet advertisingSet;
+ private final FastPairSimulator mSimulator;
+ private final BluetoothLeAdvertiser mAdvertiser;
+ private final AdvertisingSetCallback mAdvertisingSetCallback;
+ private AdvertisingSet mAdvertisingSet;
public OreoFastPairAdvertiser(FastPairSimulator simulator) {
- this.simulator = simulator;
- this.advertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
- this.advertisingSetCallback =
+ this.mSimulator = simulator;
+ this.mAdvertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
+ this.mAdvertisingSetCallback =
new AdvertisingSetCallback() {
@Override
- public void onAdvertisingSetStarted(AdvertisingSet set, int txPower, int status) {
+ public void onAdvertisingSetStarted(
+ AdvertisingSet set, int txPower, int status) {
if (status == AdvertisingSetCallback.ADVERTISE_SUCCESS) {
- logger.log("Advertising succeeded, advertising at %s dBm", txPower);
+ mLogger.log("Advertising succeeded, advertising at %s dBm", txPower);
simulator.setIsAdvertising(true);
- advertisingSet = set;
+ mAdvertisingSet = set;
try {
// Requires custom Android build, see callback below.
Reflect.on(set).withMethod("getOwnAddress").invoke();
} catch (ReflectionException e) {
- logger.log(e, "Error calling getOwnAddress for AdvertisingSet");
+ mLogger.log(e, "Error calling getOwnAddress for AdvertisingSet");
}
} else {
- logger.log(
- new IllegalStateException(), "Advertising failed, error code=%d", status);
+ mLogger.log(
+ new IllegalStateException(),
+ "Advertising failed, error code=%d", status);
}
}
@Override
public void onAdvertisingDataSet(AdvertisingSet set, int status) {
if (status != AdvertisingSetCallback.ADVERTISE_SUCCESS) {
- logger.log(
+ mLogger.log(
new IllegalStateException(),
"Updating advertisement failed, error code=%d",
status);
@@ -86,9 +89,10 @@
}
// Called via reflection with AdvertisingSet.getOwnAddress().
- public void onOwnAddressRead(AdvertisingSet set, int addressType, String address) {
+ public void onOwnAddressRead(
+ AdvertisingSet set, int addressType, String address) {
if (!address.equals(simulator.getBleAddress())) {
- logger.log(
+ mLogger.log(
"Read own BLE address=%s at %s",
address,
new SimpleDateFormat("HH:mm:ss:SSS", Locale.US)
@@ -102,21 +106,21 @@
@Override
public void startAdvertising(@Nullable byte[] serviceData) {
// To be informed that BLE address is rotated, we need to polling query it asynchronously.
- if (advertisingSet != null) {
+ if (mAdvertisingSet != null) {
try {
// Requires custom Android build, see callback: onOwnAddressRead.
- Reflect.on(advertisingSet).withMethod("getOwnAddress").invoke();
+ Reflect.on(mAdvertisingSet).withMethod("getOwnAddress").invoke();
} catch (ReflectionException ignored) {
// Ignore it due to user already knows it when setting advertisingSet.
}
}
- if (simulator.isDestroyed()) {
+ if (mSimulator.isDestroyed()) {
return;
}
if (serviceData == null) {
- logger.log("Service data is null, stop advertising");
+ mLogger.log("Service data is null, stop advertising");
stopAdvertising();
return;
}
@@ -127,10 +131,10 @@
.setIncludeTxPowerLevel(true)
.build();
- logger.log("Advertising FE2C service data=%s", base16().encode(serviceData));
+ mLogger.log("Advertising FE2C service data=%s", base16().encode(serviceData));
- if (advertisingSet != null) {
- advertisingSet.setAdvertisingData(data);
+ if (mAdvertisingSet != null) {
+ mAdvertisingSet.setAdvertisingData(data);
return;
}
@@ -141,9 +145,10 @@
.setConnectable(true)
.setScannable(true)
.setInterval(AdvertisingSetParameters.INTERVAL_LOW)
- .setTxPowerLevel(convertAdvertiseSettingsTxPower(simulator.getTxPower()))
+ .setTxPowerLevel(convertAdvertiseSettingsTxPower(mSimulator.getTxPower()))
.build();
- advertiser.startAdvertisingSet(parameters, data, null, null, null, advertisingSetCallback);
+ mAdvertiser.startAdvertisingSet(parameters, data, null, null, null,
+ mAdvertisingSetCallback);
}
private static int convertAdvertiseSettingsTxPower(int txPower) {
@@ -161,12 +166,12 @@
@Override
public void stopAdvertising() {
- if (simulator.isDestroyed()) {
+ if (mSimulator.isDestroyed()) {
return;
}
- advertiser.stopAdvertisingSet(advertisingSetCallback);
- advertisingSet = null;
- simulator.setIsAdvertising(false);
+ mAdvertiser.stopAdvertisingSet(mAdvertisingSetCallback);
+ mAdvertisingSet = null;
+ mSimulator.setIsAdvertising(false);
}
}
diff --git a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/gatt/server/BluetoothGattServerConfig.java b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/bluetooth/BluetoothGattServerConfig.java
similarity index 77%
rename from nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/gatt/server/BluetoothGattServerConfig.java
rename to nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/bluetooth/BluetoothGattServerConfig.java
index 9fa54fe..3cacd55 100644
--- a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/gatt/server/BluetoothGattServerConfig.java
+++ b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/bluetooth/BluetoothGattServerConfig.java
@@ -1,5 +1,20 @@
+/*
+ * Copyright (C) 2022 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.gatt.server;
+package android.nearby.fastpair.provider.bluetooth;
import android.annotation.TargetApi;
import android.bluetooth.BluetoothGattCharacteristic;
@@ -46,15 +61,16 @@
* TODO(lingjunl): remove them when b/21587710 is fixed.
*/
public BluetoothGattServerConfig addSelfDefinedDynamicService() {
- ServiceConfig serviceConfig = new ServiceConfig().addCharacteristic(new BluetoothGattServlet() {
- @Override
- public BluetoothGattCharacteristic getCharacteristic() {
- return new BluetoothGattCharacteristic(
- BluetoothConsts.SERVICE_DYNAMIC_CHARACTERISTIC,
- BluetoothGattCharacteristic.PROPERTY_READ,
- BluetoothGattCharacteristic.PERMISSION_READ);
- }
- });
+ ServiceConfig serviceConfig = new ServiceConfig().addCharacteristic(
+ new BluetoothGattServlet() {
+ @Override
+ public BluetoothGattCharacteristic getCharacteristic() {
+ return new BluetoothGattCharacteristic(
+ BluetoothConsts.SERVICE_DYNAMIC_CHARACTERISTIC,
+ BluetoothGattCharacteristic.PROPERTY_READ,
+ BluetoothGattCharacteristic.PERMISSION_READ);
+ }
+ });
return addService(BluetoothConsts.SERVICE_DYNAMIC_SERVICE, serviceConfig);
}
@@ -67,8 +83,8 @@
// This is not supposed to happen
throw new IllegalStateException();
}
- BluetoothGattService gattService =
- new BluetoothGattService(serviceUuid, BluetoothGattService.SERVICE_TYPE_PRIMARY);
+ BluetoothGattService gattService = new BluetoothGattService(serviceUuid,
+ BluetoothGattService.SERVICE_TYPE_PRIMARY);
for (Entry<BluetoothGattCharacteristic, BluetoothGattServlet> servletEntry :
serviceConfig.getServlets().entrySet()) {
BluetoothGattCharacteristic characteristic = servletEntry.getKey();
diff --git a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/gatt/server/BluetoothGattServerConnection.java b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/bluetooth/BluetoothGattServerConnection.java
similarity index 86%
rename from nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/gatt/server/BluetoothGattServerConnection.java
rename to nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/bluetooth/BluetoothGattServerConnection.java
index b67e00e..fae6951 100644
--- a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/gatt/server/BluetoothGattServerConnection.java
+++ b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/bluetooth/BluetoothGattServerConnection.java
@@ -1,4 +1,20 @@
-package com.android.server.nearby.common.bluetooth.gatt.server;
+/*
+ * Copyright (C) 2022 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 android.nearby.fastpair.provider.bluetooth;
import android.annotation.TargetApi;
import android.bluetooth.BluetoothGatt;
@@ -12,7 +28,6 @@
import com.android.server.nearby.common.bluetooth.BluetoothGattException;
import com.android.server.nearby.common.bluetooth.ReservedUuids;
import com.android.server.nearby.common.bluetooth.testability.android.bluetooth.BluetoothDevice;
-import com.android.server.nearby.common.bluetooth.util.BluetoothGattUtils;
import com.android.server.nearby.common.bluetooth.util.BluetoothOperationExecutor;
import com.android.server.nearby.common.bluetooth.util.BluetoothOperationExecutor.Operation;
@@ -53,7 +68,8 @@
/** Default MTU when value is unknown. */
public static final int DEFAULT_MTU = 23;
- @VisibleForTesting static final long OPERATION_TIMEOUT = TimeUnit.SECONDS.toMillis(1);
+ @VisibleForTesting
+ static final long OPERATION_TIMEOUT = TimeUnit.SECONDS.toMillis(1);
/** Notification types as defined by the BLE spec vol 4, sec G, part 3.3.3.3 */
public enum NotificationType {
@@ -72,7 +88,8 @@
private final BluetoothGattServerHelper mBluetoothGattServerHelper;
private final BluetoothDevice mBluetoothDevice;
- @VisibleForTesting BluetoothOperationExecutor mBluetoothOperationScheduler =
+ @VisibleForTesting
+ BluetoothOperationExecutor mBluetoothOperationScheduler =
new BluetoothOperationExecutor(1);
/** Stores pending writes. For each UUID, we store an offset and a byte[] of data. */
@@ -129,12 +146,13 @@
}
}
- private final BluetoothGattServlet getServlet(BluetoothGattCharacteristic characteristic)
+ private BluetoothGattServlet getServlet(BluetoothGattCharacteristic characteristic)
throws BluetoothGattException {
BluetoothGattServlet servlet = mServlets.get(characteristic);
if (servlet == null) {
throw new BluetoothGattException(
- String.format("No handler registered for characteristic %s.", characteristic.getUuid()),
+ String.format("No handler registered for characteristic %s.",
+ characteristic.getUuid()),
BluetoothGatt.GATT_REQUEST_NOT_SUPPORTED);
}
return servlet;
@@ -145,7 +163,8 @@
return getServlet(characteristic).read(this, offset);
}
- public void writeCharacteristic(BluetoothGattCharacteristic characteristic, boolean preparedWrite,
+ public void writeCharacteristic(BluetoothGattCharacteristic characteristic,
+ boolean preparedWrite,
int offset, byte[] value) throws BluetoothGattException {
Log.d(TAG, String.format(
"Received %d bytes at offset %d on %s from device %s, prepareWrite=%s.",
@@ -214,18 +233,21 @@
}
private void handleCharacteristicConfigurationChange(
- final BluetoothGattCharacteristic characteristic, BluetoothGattServlet servlet, int offset,
+ final BluetoothGattCharacteristic characteristic, BluetoothGattServlet servlet,
+ int offset,
byte[] value)
throws BluetoothGattException {
if (offset != 0) {
throw new BluetoothGattException(String.format(
- "Offset should be 0 when changing the client characteristic config: %d.", offset),
+ "Offset should be 0 when changing the client characteristic config: %d.",
+ offset),
BluetoothGatt.GATT_INVALID_OFFSET);
}
if (value.length != 2) {
throw new BluetoothGattException(String.format(
"Value 0x%s is undefined for the client characteristic config",
- BaseEncoding.base16().encode(value)), BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH);
+ BaseEncoding.base16().encode(value)),
+ BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH);
}
boolean notificationRegistered = mRegisteredNotifications.containsKey(characteristic);
@@ -268,7 +290,8 @@
default:
throw new BluetoothGattException(String.format(
"Value 0x%s is undefined for the client characteristic config",
- BaseEncoding.base16().encode(value)), BluetoothGatt.GATT_REQUEST_NOT_SUPPORTED);
+ BaseEncoding.base16().encode(value)),
+ BluetoothGatt.GATT_REQUEST_NOT_SUPPORTED);
}
}
@@ -305,27 +328,27 @@
* Assembles the specified queued writes and calls the provided write handler on the assembled
* chunks. Tries to assemble all the chunks into one write request. For example, if the content
* of byteChunks is:
- * <code>
- * offset data_size
- * 0 10
- * 10 1
- * 11 5
- * </code>
+ * <code>
+ * offset data_size
+ * 0 10
+ * 10 1
+ * 11 5
+ * </code>
*
- * then this method would call <code>writeHandler.onWrite(0, byte[16])</code>
+ * then this method would call <code>writeHandler.onWrite(0, byte[16])</code>
*
* However, if all the chunks cannot be assembled into a continuous byte[], then onWrite() will
* be called multiple times with the largest continuous chunks. For example, if the content of
* byteChunks is:
* <code>
- * offset data_size
- * 10 12
- * 30 5
- * 35 9
+ * offset data_size
+ * 10 12
+ * 30 5
+ * 35 9
* </code>
*
- * then this method would call <code>writeHandler.onWrite(10, byte[12)</code> and
- * <code>writeHandler.onWrite(30, byte[14]).
+ * then this method would call <code>writeHandler.onWrite(10, byte[12)</code> and
+ * <code>writeHandler.onWrite(30, byte[14]).
*/
private void assembleByteChunksAndHandle(BluetoothGattServlet servlet,
SortedMap<Integer, byte[]> byteChunks) throws BluetoothGattException {
@@ -336,8 +359,9 @@
Integer offset = byteChunks.firstKey();
if (offset.intValue() < startWritingAtOffset + assembledRequest.size()) {
- throw new BluetoothGattException("Expected offset of at least " + assembledRequest.size()
- + ", but got offset " + offset, BluetoothGatt.GATT_INVALID_OFFSET);
+ throw new BluetoothGattException(
+ "Expected offset of at least " + assembledRequest.size()
+ + ", but got offset " + offset, BluetoothGatt.GATT_INVALID_OFFSET);
}
// If we have a hole, then write what we've already assembled and start assembling a new
@@ -357,7 +381,8 @@
}
assembledRequest.write(dataChunk);
} catch (IOException e) {
- throw new BluetoothGattException("Error assembling request", BluetoothGatt.GATT_FAILURE);
+ throw new BluetoothGattException("Error assembling request",
+ BluetoothGatt.GATT_FAILURE);
}
}
@@ -375,8 +400,10 @@
new Operation<Void>(OperationType.SEND_NOTIFICATION) {
@Override
public void run() throws BluetoothException {
- mBluetoothGattServerHelper.sendNotification(mBluetoothDevice, characteristic,
- data, notificationType == NotificationType.INDICATION ? true : false);
+ mBluetoothGattServerHelper.sendNotification(mBluetoothDevice,
+ characteristic,
+ data,
+ notificationType == NotificationType.INDICATION ? true : false);
}
},
OPERATION_TIMEOUT);
@@ -409,7 +436,7 @@
private final Object mScope;
private final String mKey;
- public ScopedKey(Object scope, String key) {
+ ScopedKey(Object scope, String key) {
mScope = scope;
mKey = key;
}
@@ -430,12 +457,12 @@
}
/** Listener to be notified when the connection closes. */
- public static interface Listener {
+ public interface Listener {
void onClose();
}
/** Notifier to notify data over notification or indication. */
- public static interface Notifier {
+ public interface Notifier {
void notify(byte[] data) throws BluetoothException;
}
}
diff --git a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/gatt/server/BluetoothGattServerHelper.java b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/bluetooth/BluetoothGattServerHelper.java
similarity index 77%
rename from nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/gatt/server/BluetoothGattServerHelper.java
rename to nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/bluetooth/BluetoothGattServerHelper.java
index 0ba96c0..9339e14 100644
--- a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/gatt/server/BluetoothGattServerHelper.java
+++ b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/bluetooth/BluetoothGattServerHelper.java
@@ -1,4 +1,20 @@
-package com.android.server.nearby.common.bluetooth.gatt.server;
+/*
+ * Copyright (C) 2022 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 android.nearby.fastpair.provider.bluetooth;
import android.annotation.TargetApi;
import android.bluetooth.BluetoothGatt;
@@ -19,8 +35,6 @@
import com.android.server.nearby.common.bluetooth.testability.android.bluetooth.BluetoothDevice;
import com.android.server.nearby.common.bluetooth.testability.android.bluetooth.BluetoothGattServer;
import com.android.server.nearby.common.bluetooth.testability.android.bluetooth.BluetoothGattServerCallback;
-import com.android.server.nearby.common.bluetooth.testability.android.bluetooth.BluetoothManager;
-import com.android.server.nearby.common.bluetooth.util.BluetoothGattUtils;
import com.android.server.nearby.common.bluetooth.util.BluetoothOperationExecutor;
import com.android.server.nearby.common.bluetooth.util.BluetoothOperationExecutor.Operation;
@@ -37,7 +51,8 @@
public class BluetoothGattServerHelper {
private static final String TAG = BluetoothGattServerHelper.class.getSimpleName();
- @VisibleForTesting static final long OPERATION_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(1);
+ @VisibleForTesting
+ static final long OPERATION_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(1);
private static final int MAX_PARALLEL_OPERATIONS = 5;
/** BT operation types that can be in flight. */
@@ -48,9 +63,11 @@
}
private final Object mOperationLock = new Object();
- @VisibleForTesting final BluetoothGattServerCallback mGattServerCallback =
+ @VisibleForTesting
+ final BluetoothGattServerCallback mGattServerCallback =
new GattServerCallback();
- @VisibleForTesting BluetoothOperationExecutor mBluetoothOperationScheduler =
+ @VisibleForTesting
+ BluetoothOperationExecutor mBluetoothOperationScheduler =
new BluetoothOperationExecutor(MAX_PARALLEL_OPERATIONS);
private final Context mContext;
@@ -58,12 +75,15 @@
private final VersionProvider mVersionProvider;
@Nullable
- @VisibleForTesting volatile BluetoothGattServerConfig mServerConfig = null;
+ @VisibleForTesting
+ volatile BluetoothGattServerConfig mServerConfig = null;
@Nullable
- @VisibleForTesting volatile BluetoothGattServer mBluetoothGattServer = null;
+ @VisibleForTesting
+ volatile BluetoothGattServer mBluetoothGattServer = null;
- @VisibleForTesting final ConcurrentMap<BluetoothDevice, BluetoothGattServerConnection>
+ @VisibleForTesting
+ final ConcurrentMap<BluetoothDevice, BluetoothGattServerConnection>
mConnections = new ConcurrentHashMap<BluetoothDevice, BluetoothGattServerConnection>();
public BluetoothGattServerHelper(Context context, BluetoothManager bluetoothManager) {
@@ -74,7 +94,8 @@
);
}
- @VisibleForTesting BluetoothGattServerHelper(
+ @VisibleForTesting
+ BluetoothGattServerHelper(
Context context, BluetoothManager bluetoothManager, VersionProvider versionProvider) {
mContext = context;
mBluetoothManager = bluetoothManager;
@@ -97,7 +118,8 @@
}
try {
- for (final BluetoothGattService service : gattServerConfig.getBluetoothGattServices()) {
+ for (final BluetoothGattService service :
+ gattServerConfig.getBluetoothGattServices()) {
if (service == null) {
continue;
}
@@ -165,7 +187,8 @@
if (bluetoothGattServer == null) {
throw new BluetoothException("Server is not open.");
}
- BluetoothGattCharacteristic clonedCharacteristic = BluetoothGattUtils.clone(characteristic);
+ BluetoothGattCharacteristic clonedCharacteristic =
+ BluetoothGattUtils.clone(characteristic);
clonedCharacteristic.setValue(data);
bluetoothGattServer.notifyCharacteristicChanged(device, clonedCharacteristic, confirm);
}
@@ -181,12 +204,13 @@
if (connectionSate != BluetoothGatt.STATE_CONNECTED) {
return;
}
- mBluetoothOperationScheduler.execute(new Operation<Void>(OperationType.CLOSE_CONNECTION) {
- @Override
- public void run() throws BluetoothException {
- bluetoothGattServer.cancelConnection(bluetoothDevice);
- }
- },
+ mBluetoothOperationScheduler.execute(
+ new Operation<Void>(OperationType.CLOSE_CONNECTION) {
+ @Override
+ public void run() throws BluetoothException {
+ bluetoothGattServer.cancelConnection(bluetoothDevice);
+ }
+ },
OPERATION_TIMEOUT_MILLIS);
}
@@ -214,8 +238,8 @@
}
Log.i(TAG, String.format("Connected to device %s.", device));
if (mConnections.containsKey(device)) {
- Log.w(TAG, String.format(
- "A connection is already open with device %s. Keeping existing one.", device));
+ Log.w(TAG, String.format("A connection is already open with device %s. "
+ + "Keeping existing one.", device));
return;
}
@@ -228,28 +252,35 @@
}
mConnections.put(device, connection);
- // By default, Android disconnects active GATT server connection if the advertisement is
- // stop (or sometime stopScanning also disconnect, see b/62667394). Asking the server to
+ // By default, Android disconnects active GATT server connection if the
+ // advertisement is
+ // stop (or sometime stopScanning also disconnect, see b/62667394). Asking
+ // the server to
// reverse connect will tell Android to keep the connection open.
// Code handling connect() on Android OS is: btif_gatt_server.c
// Note: for Android < P, unknown type devices don't connect. See b/62827460.
- // TODO(mfucci): this can be fixed if the GATT server is forced to be LE only.
- // for Android P+, unknown type devices always use LE to connect (see code)
- // Note: for Android < N, dual mode devices always connect using BT classic, so connect()
+ // for Android P+, unknown type devices always use LE to connect (see
+ // code)
+ // Note: for Android < N, dual mode devices always connect using BT classic,
+ // so connect()
// should *NOT* be called for those devices. See b/29819614.
if (mVersionProvider.getSdkInt() >= VERSION_CODES.N
|| device.getType() != BluetoothDevice.DEVICE_TYPE_DUAL) {
- boolean success = bluetoothGattServer.connect(device, /* autoConnect */false);
+ boolean success = bluetoothGattServer.connect(device, /* autoConnect */
+ false);
if (!success) {
Log.w(TAG, String.format(
- "Keeping connection open on stop advertising failed for device %s.", device));
+ "Keeping connection open on stop advertising failed for "
+ + "device %s.",
+ device));
}
}
break;
case BluetoothGattServer.STATE_DISCONNECTED:
if (status != BluetoothGatt.GATT_SUCCESS) {
- Log.w(TAG, String.format("Disconnection from %s error: %s. Proceeding anyway.", device,
- BluetoothGattUtils.getMessageForStatusCode(status)));
+ Log.w(TAG, String.format(
+ "Disconnection from %s error: %s. Proceeding anyway.",
+ device, BluetoothGattUtils.getMessageForStatusCode(status)));
}
bluetoothLeConnection = mConnections.remove(device);
if (bluetoothLeConnection != null) {
@@ -262,7 +293,6 @@
break;
default:
Log.e(TAG, String.format("Unexpected connection state: %d", newState));
- return;
}
}
@@ -274,8 +304,10 @@
return;
}
try {
- byte[] value = getConnectionByDevice(device).readCharacteristic(offset, characteristic);
- bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset,
+ byte[] value =
+ getConnectionByDevice(device).readCharacteristic(offset, characteristic);
+ bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS,
+ offset,
value);
} catch (BluetoothGattException e) {
Log.e(TAG,
@@ -285,7 +317,8 @@
device,
offset),
e);
- bluetoothGattServer.sendResponse(device, requestId, e.getGattErrorCode(), offset, null);
+ bluetoothGattServer.sendResponse(
+ device, requestId, e.getGattErrorCode(), offset, null);
}
}
@@ -307,8 +340,8 @@
offset,
value);
if (responseNeeded) {
- bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset,
- null);
+ bluetoothGattServer.sendResponse(
+ device, requestId, BluetoothGatt.GATT_SUCCESS, offset, null);
}
} catch (BluetoothGattException e) {
Log.e(TAG,
@@ -318,7 +351,8 @@
device,
offset),
e);
- bluetoothGattServer.sendResponse(device, requestId, e.getGattErrorCode(), offset, null);
+ bluetoothGattServer.sendResponse(
+ device, requestId, e.getGattErrorCode(), offset, null);
}
}
@@ -331,8 +365,8 @@
}
try {
byte[] value = getConnectionByDevice(device).readDescriptor(offset, descriptor);
- bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset,
- value);
+ bluetoothGattServer.sendResponse(
+ device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
} catch (BluetoothGattException e) {
Log.e(TAG, String.format(
"Could not read %s on device %s at %d",
@@ -340,7 +374,8 @@
device,
offset),
e);
- bluetoothGattServer.sendResponse(device, requestId, e.getGattErrorCode(), offset, null);
+ bluetoothGattServer.sendResponse(
+ device, requestId, e.getGattErrorCode(), offset, null);
}
}
@@ -357,10 +392,11 @@
return;
}
try {
- getConnectionByDevice(device).writeDescriptor(descriptor, preparedWrite, offset, value);
+ getConnectionByDevice(device)
+ .writeDescriptor(descriptor, preparedWrite, offset, value);
if (responseNeeded) {
- bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset,
- null);
+ bluetoothGattServer.sendResponse(
+ device, requestId, BluetoothGatt.GATT_SUCCESS, offset, null);
}
Log.d(TAG, "Operation onDescriptorWriteRequest successful.");
} catch (BluetoothGattException e) {
@@ -371,7 +407,8 @@
device,
offset),
e);
- bluetoothGattServer.sendResponse(device, requestId, e.getGattErrorCode(), offset, null);
+ bluetoothGattServer.sendResponse(
+ device, requestId, e.getGattErrorCode(), offset, null);
}
}
@@ -383,7 +420,8 @@
}
try {
getConnectionByDevice(device).executeWrite(execute);
- bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, null);
+ bluetoothGattServer.sendResponse(
+ device, requestId, BluetoothGatt.GATT_SUCCESS, 0, null);
} catch (BluetoothGattException e) {
Log.e(TAG, "Could not execute write.", e);
bluetoothGattServer.sendResponse(device, requestId, e.getGattErrorCode(), 0, null);
@@ -392,12 +430,13 @@
@Override
public void onNotificationSent(BluetoothDevice device, int status) {
- Log.d(TAG, String.format("Received onNotificationSent for device %s with status %s", device,
- status));
+ Log.d(TAG,
+ String.format("Received onNotificationSent for device %s with status %s",
+ device, status));
try {
getConnectionByDevice(device).notifyNotificationSent(status);
} catch (BluetoothGattException e) {
- Log.e(TAG, String.format("An error occurred when receiving onNotificationSent"), e);
+ Log.e(TAG, "An error occurred when receiving onNotificationSent: " + e);
}
}
}
diff --git a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/gatt/server/BluetoothGattServlet.java b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/bluetooth/BluetoothGattServlet.java
similarity index 75%
rename from nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/gatt/server/BluetoothGattServlet.java
rename to nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/bluetooth/BluetoothGattServlet.java
index c4a2473..e25e223 100644
--- a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/gatt/server/BluetoothGattServlet.java
+++ b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/bluetooth/BluetoothGattServlet.java
@@ -1,12 +1,28 @@
-package com.android.server.nearby.common.bluetooth.gatt.server;
+/*
+ * Copyright (C) 2022 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 android.nearby.fastpair.provider.bluetooth;
import android.annotation.TargetApi;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
+import android.nearby.fastpair.provider.bluetooth.BluetoothGattServerConnection.Notifier;
import com.android.server.nearby.common.bluetooth.BluetoothGattException;
-import com.android.server.nearby.common.bluetooth.gatt.server.BluetoothGattServerConnection.Notifier;
/** Servlet to handle GATT operations on a characteristic. */
@TargetApi(18)
diff --git a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/util/BluetoothGattUtils.java b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/bluetooth/BluetoothGattUtils.java
similarity index 98%
rename from nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/util/BluetoothGattUtils.java
rename to nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/bluetooth/BluetoothGattUtils.java
index e90b732..7ac26ee 100644
--- a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/util/BluetoothGattUtils.java
+++ b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/bluetooth/BluetoothGattUtils.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package com.android.server.nearby.common.bluetooth.util;
+package android.nearby.fastpair.provider.bluetooth;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
diff --git a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/testability/android/bluetooth/BluetoothManager.java b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/bluetooth/BluetoothManager.java
similarity index 83%
rename from nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/testability/android/bluetooth/BluetoothManager.java
rename to nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/bluetooth/BluetoothManager.java
index 4ccfb59..bf241f1 100644
--- a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/testability/android/bluetooth/BluetoothManager.java
+++ b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/bluetooth/BluetoothManager.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2021 The Android Open Source Project
+ * Copyright (C) 2022 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.
@@ -14,12 +14,17 @@
* limitations under the License.
*/
-package com.android.server.nearby.common.bluetooth.testability.android.bluetooth;
+package android.nearby.fastpair.provider.bluetooth;
import android.content.Context;
import androidx.annotation.Nullable;
+import com.android.server.nearby.common.bluetooth.testability.android.bluetooth.BluetoothAdapter;
+import com.android.server.nearby.common.bluetooth.testability.android.bluetooth.BluetoothDevice;
+import com.android.server.nearby.common.bluetooth.testability.android.bluetooth.BluetoothGattServer;
+import com.android.server.nearby.common.bluetooth.testability.android.bluetooth.BluetoothGattServerCallback;
+
import java.util.ArrayList;
import java.util.List;
@@ -72,4 +77,4 @@
public static BluetoothManager wrap(android.bluetooth.BluetoothManager bluetoothManager) {
return new BluetoothManager(bluetoothManager);
}
-}
\ No newline at end of file
+}
diff --git a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/RfcommServer.java b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/bluetooth/RfcommServer.java
similarity index 76%
rename from nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/RfcommServer.java
rename to nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/bluetooth/RfcommServer.java
index 7846226..9ed95ac 100644
--- a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/RfcommServer.java
+++ b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/bluetooth/RfcommServer.java
@@ -14,20 +14,21 @@
* limitations under the License.
*/
-package com.android.server.nearby.common.bluetooth.fastpair.testing;
+package android.nearby.fastpair.provider.bluetooth;
-import static com.android.server.nearby.common.bluetooth.fastpair.testing.RfcommServer.State.ACCEPTING;
-import static com.android.server.nearby.common.bluetooth.fastpair.testing.RfcommServer.State.CONNECTED;
-import static com.android.server.nearby.common.bluetooth.fastpair.testing.RfcommServer.State.RESTARTING;
-import static com.android.server.nearby.common.bluetooth.fastpair.testing.RfcommServer.State.STARTING;
-import static com.android.server.nearby.common.bluetooth.fastpair.testing.RfcommServer.State.STOPPED;
+import static android.nearby.fastpair.provider.bluetooth.RfcommServer.State.ACCEPTING;
+import static android.nearby.fastpair.provider.bluetooth.RfcommServer.State.CONNECTED;
+import static android.nearby.fastpair.provider.bluetooth.RfcommServer.State.RESTARTING;
+import static android.nearby.fastpair.provider.bluetooth.RfcommServer.State.STARTING;
+import static android.nearby.fastpair.provider.bluetooth.RfcommServer.State.STOPPED;
import static java.nio.charset.StandardCharsets.UTF_8;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
-import android.nearby.multidevices.fastpair.EventStreamProtocol;
+import android.nearby.fastpair.provider.EventStreamProtocol;
+import android.nearby.fastpair.provider.utils.Logger;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
@@ -46,33 +47,33 @@
*/
public class RfcommServer {
private static final String TAG = "RfcommServer";
- private final Logger logger = new Logger(TAG);
+ private final Logger mLogger = new Logger(TAG);
private static final String FAST_PAIR_RFCOMM_SERVICE_NAME = "FastPairServer";
public static final UUID FAST_PAIR_RFCOMM_UUID =
UUID.fromString("df21fe2c-2515-4fdb-8886-f12c4d67927c");
/** A single thread executor where all state checks are performed. */
- private final ExecutorService controllerExecutor = Executors.newSingleThreadExecutor();
+ private final ExecutorService mControllerExecutor = Executors.newSingleThreadExecutor();
- private final ExecutorService sendMessageExecutor = Executors.newSingleThreadExecutor();
- private final ExecutorService receiveMessageExecutor = Executors.newSingleThreadExecutor();
+ private final ExecutorService mSendMessageExecutor = Executors.newSingleThreadExecutor();
+ private final ExecutorService mReceiveMessageExecutor = Executors.newSingleThreadExecutor();
@Nullable
- private BluetoothServerSocket serverSocket;
+ private BluetoothServerSocket mServerSocket;
@Nullable
- private BluetoothSocket socket;
+ private BluetoothSocket mSocket;
- private State state = STOPPED;
- private boolean isStopRequested = false;
+ private State mState = STOPPED;
+ private boolean mIsStopRequested = false;
@Nullable
- private RequestHandler requestHandler;
+ private RequestHandler mRequestHandler;
@Nullable
- private CountDownLatch countDownLatch;
+ private CountDownLatch mCountDownLatch;
@Nullable
- private StateMonitor stateMonitor;
+ private StateMonitor mStateMonitor;
/**
* Manages RfcommServer status.
@@ -108,12 +109,12 @@
private void startServer() {
log("Start RfcommServer");
- if (!state.equals(STOPPED)) {
+ if (!mState.equals(STOPPED)) {
log("Server is not stopped, skip start request.");
return;
}
updateState(STARTING);
- isStopRequested = false;
+ mIsStopRequested = false;
startAccept();
}
@@ -127,7 +128,7 @@
private void startAccept() {
try {
// Gets server socket in controller thread for stop() API.
- serverSocket =
+ mServerSocket =
BluetoothAdapter.getDefaultAdapter()
.listenUsingRfcommWithServiceRecord(
FAST_PAIR_RFCOMM_SERVICE_NAME, FAST_PAIR_RFCOMM_UUID);
@@ -138,7 +139,7 @@
}
updateState(ACCEPTING);
- new Thread(() -> accept(serverSocket)).start();
+ new Thread(() -> accept(mServerSocket)).start();
}
private void accept(BluetoothServerSocket serverSocket) {
@@ -156,7 +157,7 @@
}
private void handleAcceptException(BluetoothServerSocket serverSocket) {
- if (isStopRequested) {
+ if (mIsStopRequested) {
stopServer();
} else {
closeServerSocket(serverSocket);
@@ -165,7 +166,7 @@
}
private void startListen(BluetoothSocket bluetoothSocket) {
- if (isStopRequested) {
+ if (mIsStopRequested) {
closeSocket(bluetoothSocket);
stopServer();
return;
@@ -173,7 +174,7 @@
updateState(CONNECTED);
// Sets method parameter to global socket for stop() API.
- this.socket = bluetoothSocket;
+ this.mSocket = bluetoothSocket;
new Thread(() -> listen(bluetoothSocket)).start();
}
@@ -195,11 +196,12 @@
} while (count < additionalLength);
}
- if (requestHandler != null) {
- // In order not to block listening thread, use different thread to dispatch message.
- receiveMessageExecutor.execute(
+ if (mRequestHandler != null) {
+ // In order not to block listening thread, use different thread to dispatch
+ // message.
+ mReceiveMessageExecutor.execute(
() -> {
- requestHandler.handleRequest(eventGroup, eventCode, data);
+ mRequestHandler.handleRequest(eventGroup, eventCode, data);
triggerCountdownLatch();
});
}
@@ -207,13 +209,14 @@
} catch (IOException e) {
log(
String.format(
- "IOException when listening to %s", bluetoothSocket.getRemoteDevice().getAddress()));
+ "IOException when listening to %s",
+ bluetoothSocket.getRemoteDevice().getAddress()));
runInControllerExecutor(() -> handleListenException(bluetoothSocket));
}
}
private void handleListenException(BluetoothSocket bluetoothSocket) {
- if (isStopRequested) {
+ if (mIsStopRequested) {
stopServer();
} else {
closeSocket(bluetoothSocket);
@@ -225,15 +228,18 @@
switch (eventGroup) {
case BLUETOOTH:
send(EventStreamProtocol.EventGroup.BLUETOOTH_VALUE,
- EventStreamProtocol.BluetoothEventCode.BLUETOOTH_ENABLE_SILENCE_MODE_VALUE, new byte[0]);
+ EventStreamProtocol.BluetoothEventCode.BLUETOOTH_ENABLE_SILENCE_MODE_VALUE,
+ new byte[0]);
break;
case LOGGING:
- send(EventStreamProtocol.EventGroup.LOGGING_VALUE, EventStreamProtocol.LoggingEventCode.LOG_FULL_VALUE,
+ send(EventStreamProtocol.EventGroup.LOGGING_VALUE,
+ EventStreamProtocol.LoggingEventCode.LOG_FULL_VALUE,
new byte[0]);
break;
case DEVICE:
send(EventStreamProtocol.EventGroup.DEVICE_VALUE,
- EventStreamProtocol.DeviceEventCode.DEVICE_BATTERY_INFO_VALUE, new byte[]{0x11, 0x12, 0x13});
+ EventStreamProtocol.DeviceEventCode.DEVICE_BATTERY_INFO_VALUE,
+ new byte[]{0x11, 0x12, 0x13});
break;
default: // fall out
}
@@ -248,12 +254,12 @@
public void send(int eventGroup, int eventCode, byte[] data) {
runInControllerExecutor(
() -> {
- if (!CONNECTED.equals(state)) {
+ if (!CONNECTED.equals(mState)) {
log("Server is not in CONNECTED state, skip send request");
return;
}
- BluetoothSocket bluetoothSocket = this.socket;
- sendMessageExecutor.execute(() -> {
+ BluetoothSocket bluetoothSocket = this.mSocket;
+ mSendMessageExecutor.execute(() -> {
String address = bluetoothSocket.getRemoteDevice().getAddress();
try {
DataOutputStream dataOutputStream =
@@ -285,23 +291,23 @@
runInControllerExecutor(() -> {
log("Stop RfcommServer");
- if (STOPPED.equals(state)) {
+ if (STOPPED.equals(mState)) {
log("Server is stopped, skip stop request.");
return;
}
- if (isStopRequested) {
+ if (mIsStopRequested) {
log("Stop is already requested, skip stop request.");
return;
}
- isStopRequested = true;
+ mIsStopRequested = true;
- if (ACCEPTING.equals(state)) {
- closeServerSocket(serverSocket);
+ if (ACCEPTING.equals(mState)) {
+ closeServerSocket(mServerSocket);
}
- if (CONNECTED.equals(state)) {
- closeSocket(socket);
+ if (CONNECTED.equals(mState)) {
+ closeSocket(mSocket);
}
});
}
@@ -312,11 +318,11 @@
}
private void updateState(State newState) {
- log(String.format("Change state from %s to %s", state, newState));
- if (stateMonitor != null) {
- stateMonitor.onStateChanged(newState);
+ log(String.format("Change state from %s to %s", mState, newState));
+ if (mStateMonitor != null) {
+ mStateMonitor.onStateChanged(newState);
}
- state = newState;
+ mState = newState;
}
private void closeServerSocket(BluetoothServerSocket serverSocket) {
@@ -342,25 +348,26 @@
socket.close();
}
} catch (IOException e) {
- log(String.format("IOException when close socket %s", socket.getRemoteDevice().getAddress()));
+ log(String.format("IOException when close socket %s",
+ socket.getRemoteDevice().getAddress()));
}
}
private void runInControllerExecutor(Runnable runnable) {
- controllerExecutor.execute(runnable);
+ mControllerExecutor.execute(runnable);
}
private void log(String message) {
- logger.log("Server=%s, %s", FAST_PAIR_RFCOMM_SERVICE_NAME, message);
+ mLogger.log("Server=%s, %s", FAST_PAIR_RFCOMM_SERVICE_NAME, message);
}
private void log(String message, Throwable e) {
- logger.log(e, "Server=%s, %s", FAST_PAIR_RFCOMM_SERVICE_NAME, message);
+ mLogger.log(e, "Server=%s, %s", FAST_PAIR_RFCOMM_SERVICE_NAME, message);
}
private void triggerCountdownLatch() {
- if (countDownLatch != null) {
- countDownLatch.countDown();
+ if (mCountDownLatch != null) {
+ mCountDownLatch.countDown();
}
}
@@ -370,7 +377,7 @@
}
public void setRequestHandler(@Nullable RequestHandler requestHandler) {
- this.requestHandler = requestHandler;
+ this.mRequestHandler = requestHandler;
}
/** A state monitor to send signal when state is changed. */
@@ -379,24 +386,24 @@
}
public void setStateMonitor(@Nullable StateMonitor stateMonitor) {
- this.stateMonitor = stateMonitor;
+ this.mStateMonitor = stateMonitor;
}
@VisibleForTesting
void setCountDownLatch(@Nullable CountDownLatch countDownLatch) {
- this.countDownLatch = countDownLatch;
+ this.mCountDownLatch = countDownLatch;
}
@VisibleForTesting
void setIsStopRequested(boolean isStopRequested) {
- this.isStopRequested = isStopRequested;
+ this.mIsStopRequested = isStopRequested;
}
@VisibleForTesting
void simulateAcceptIOException() {
runInControllerExecutor(() -> {
- if (ACCEPTING.equals(state)) {
- closeServerSocket(serverSocket);
+ if (ACCEPTING.equals(mState)) {
+ closeServerSocket(mServerSocket);
}
});
}
@@ -404,8 +411,8 @@
@VisibleForTesting
void simulateListenIOException() {
runInControllerExecutor(() -> {
- if (CONNECTED.equals(state)) {
- closeSocket(socket);
+ if (CONNECTED.equals(mState)) {
+ closeSocket(mSocket);
}
});
}
diff --git a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/Crypto.java b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/crypto/Crypto.java
similarity index 97%
rename from nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/Crypto.java
rename to nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/crypto/Crypto.java
index 1543953..0aa4f6e 100644
--- a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/Crypto.java
+++ b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/crypto/Crypto.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package com.android.server.nearby.common.bluetooth.fastpair.testing;
+package android.nearby.fastpair.provider.crypto;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
diff --git a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/E2eeCalculator.java b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/crypto/E2eeCalculator.java
similarity index 89%
rename from nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/E2eeCalculator.java
rename to nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/crypto/E2eeCalculator.java
index 6f213e6..794c19d 100644
--- a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/E2eeCalculator.java
+++ b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/crypto/E2eeCalculator.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package com.android.server.nearby.common.bluetooth.fastpair.testing;
+package android.nearby.fastpair.provider.crypto;
import com.google.common.base.Preconditions;
import com.google.common.base.Verify;
@@ -43,18 +43,18 @@
private static final int E2EE_EID_SIZE = 20;
/**
- * Computes the E2EE EID value for the given device clock based time. Note that Eddystone beacons
- * start advertising the new EID at a random time within the window, therefore the currently
- * advertised EID for beacon time <em>t</em> may be either {@code computeE2eeEid(eik, k, t)} or
- * {@code computeE2eeEid(eik, k, t - (1 << k))}.
+ * Computes the E2EE EID value for the given device clock based time. Note that Eddystone
+ * beacons start advertising the new EID at a random time within the window, therefore the
+ * currently advertised EID for beacon time <em>t</em> may be either
+ * {@code computeE2eeEid(eik, k, t)} or {@code computeE2eeEid(eik, k, t - (1 << k))}.
*
* <p>The E2EE EID computation is based on https://goto.google.com/e2ee-eid-computation.
*
* @param identityKey the beacon's 32-byte Eddystone E2EE identity key
- * @param exponent rotation period exponent as configured on the beacon, must be in range the [0,
- * 15]
- * @param deviceClockSeconds the value of the beacon's 32-bit seconds time counter (treated as an
- * unsigned value)
+ * @param exponent rotation period exponent as configured on the beacon, must be in
+ * range the [0,15]
+ * @param deviceClockSeconds the value of the beacon's 32-bit seconds time counter (treated as
+ * an unsigned value)
* @return E2EE EID value.
*/
public static ByteString computeE2eeEid(
@@ -96,8 +96,8 @@
byte[] unalignedBytes = point.getAffineX().toByteArray();
// The unalignedBytes may have length < 32 if the leading E2EE EID bytes are zero, or
- // it may be E2EE_EID_SIZE + 1 if the leading bit is 1, in which case the first byte is always
- // zero.
+ // it may be E2EE_EID_SIZE + 1 if the leading bit is 1, in which case the first byte is
+ // always zero.
Verify.verify(
unalignedBytes.length <= E2EE_EID_SIZE
|| (unalignedBytes.length == E2EE_EID_SIZE + 1 && unalignedBytes[0] == 0));
@@ -106,7 +106,8 @@
if (unalignedBytes.length < E2EE_EID_SIZE) {
bytes = new byte[E2EE_EID_SIZE];
System.arraycopy(
- unalignedBytes, 0, bytes, bytes.length - unalignedBytes.length, unalignedBytes.length);
+ unalignedBytes, 0, bytes, bytes.length - unalignedBytes.length,
+ unalignedBytes.length);
} else if (unalignedBytes.length == E2EE_EID_SIZE + 1) {
bytes = new byte[E2EE_EID_SIZE];
System.arraycopy(unalignedBytes, 1, bytes, 0, E2EE_EID_SIZE);
@@ -161,7 +162,8 @@
.subtract(s.getAffineY())
.multiply(r.getAffineX().subtract(s.getAffineX()).modInverse(P))
.mod(P);
- BigInteger x = slope.modPow(TWO, P).subtract(r.getAffineX()).subtract(s.getAffineX()).mod(P);
+ BigInteger x =
+ slope.modPow(TWO, P).subtract(r.getAffineX()).subtract(s.getAffineX()).mod(P);
BigInteger y = s.getAffineY().negate().mod(P);
y = y.add(slope.multiply(s.getAffineX().subtract(x))).mod(P);
return new ECPoint(x, y);
@@ -176,7 +178,8 @@
slope = slope.add(CURVE_SPEC.getCurve().getA());
slope = slope.multiply(r.getAffineY().multiply(TWO).modInverse(P));
BigInteger x = slope.pow(2).subtract(r.getAffineX().multiply(TWO)).mod(P);
- BigInteger y = r.getAffineY().negate().add(slope.multiply(r.getAffineX().subtract(x))).mod(P);
+ BigInteger y =
+ r.getAffineY().negate().add(slope.multiply(r.getAffineX().subtract(x))).mod(P);
return new ECPoint(x, y);
}
diff --git a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/Logger.java b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/utils/Logger.java
similarity index 79%
rename from nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/Logger.java
rename to nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/utils/Logger.java
index 37b065f..794f100 100644
--- a/nearby/tests/multidevices/clients/src/com/android/server/nearby/common/bluetooth/fastpair/testing/Logger.java
+++ b/nearby/tests/multidevices/clients/test_support/fastpair_provider/src/android/nearby/fastpair/provider/utils/Logger.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package com.android.server.nearby.common.bluetooth.fastpair.testing;
+package android.nearby.fastpair.provider.utils;
import android.util.Log;
@@ -26,10 +26,10 @@
* The base context for a logging statement.
*/
public class Logger {
- private final String tag;
+ private final String mString;
public Logger(String tag) {
- this.tag = tag;
+ this.mString = tag;
}
@FormatMethod
@@ -41,10 +41,10 @@
@FormatMethod
public void log(@Nullable Throwable exception, String message, Object... objects) {
if (exception == null) {
- Log.i(tag, String.format(message, objects));
+ Log.i(mString, String.format(message, objects));
} else {
- Log.w(tag, String.format(message, objects));
- Log.w(tag, String.format("Cause: %s", exception));
+ Log.w(mString, String.format(message, objects));
+ Log.w(mString, String.format("Cause: %s", exception));
}
}
}
diff --git a/nearby/tests/multidevices/clients/proto/Android.bp b/nearby/tests/multidevices/clients/test_support/snippet_helper/Android.bp
similarity index 73%
copy from nearby/tests/multidevices/clients/proto/Android.bp
copy to nearby/tests/multidevices/clients/test_support/snippet_helper/Android.bp
index 80e09b4..697c88d 100644
--- a/nearby/tests/multidevices/clients/proto/Android.bp
+++ b/nearby/tests/multidevices/clients/test_support/snippet_helper/Android.bp
@@ -16,15 +16,9 @@
default_applicable_licenses: ["Android-Apache-2.0"],
}
-java_library {
- name: "NearbyMultiDevicesClientsFastPairLiteProtos",
- proto: {
- type: "lite",
- canonical_path_from_root: false,
- },
- sdk_version: "system_current",
- min_sdk_version: "30",
- srcs: ["src/*/*.proto"],
+android_library {
+ name: "MoblySnippetHelperLib",
+ srcs: ["src/**/*.kt"],
+ sdk_version: "test_current",
+ static_libs: ["mobly-snippet-lib",],
}
-
-
diff --git a/nearby/tests/multidevices/clients/test_support/snippet_helper/AndroidManifest.xml b/nearby/tests/multidevices/clients/test_support/snippet_helper/AndroidManifest.xml
new file mode 100644
index 0000000..4858f46
--- /dev/null
+++ b/nearby/tests/multidevices/clients/test_support/snippet_helper/AndroidManifest.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ ~ Copyright (C) 2022 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.
+ -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.google.android.mobly.snippet.util">
+
+</manifest>
diff --git a/nearby/tests/multidevices/clients/src/android/nearby/multidevices/common/SnippetEventHelper.kt b/nearby/tests/multidevices/clients/test_support/snippet_helper/src/com/google/android/mobly/snippet/util/SnippetEventHelper.kt
similarity index 96%
rename from nearby/tests/multidevices/clients/src/android/nearby/multidevices/common/SnippetEventHelper.kt
rename to nearby/tests/multidevices/clients/test_support/snippet_helper/src/com/google/android/mobly/snippet/util/SnippetEventHelper.kt
index c4816fb..0dbcb57 100644
--- a/nearby/tests/multidevices/clients/src/android/nearby/multidevices/common/SnippetEventHelper.kt
+++ b/nearby/tests/multidevices/clients/test_support/snippet_helper/src/com/google/android/mobly/snippet/util/SnippetEventHelper.kt
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package android.nearby.multidevices.common
+package com.google.android.mobly.snippet.util
import android.os.Bundle
import com.google.android.mobly.snippet.event.EventCache
diff --git a/nearby/tests/multidevices/clients/test_support/snippet_helper/tests/Android.bp b/nearby/tests/multidevices/clients/test_support/snippet_helper/tests/Android.bp
new file mode 100644
index 0000000..284d5c2
--- /dev/null
+++ b/nearby/tests/multidevices/clients/test_support/snippet_helper/tests/Android.bp
@@ -0,0 +1,38 @@
+// Copyright (C) 2022 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 {
+ default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+// Run the tests: atest --host MoblySnippetHelperRoboTest
+android_robolectric_test {
+ name: "MoblySnippetHelperRoboTest",
+ srcs: ["src/**/*.kt"],
+ instrumentation_for: "NearbyMultiDevicesClientsSnippets",
+ java_resources: ["robolectric.properties"],
+
+ static_libs: [
+ "MoblySnippetHelperLib",
+ "androidx.test.ext.junit",
+ "androidx.test.rules",
+ "junit",
+ "mobly-snippet-lib",
+ "truth-prebuilt",
+ ],
+ test_options: {
+ // timeout in seconds.
+ timeout: 36000,
+ },
+}
\ No newline at end of file
diff --git a/nearby/tests/multidevices/clients/test_support/snippet_helper/tests/AndroidManifest.xml b/nearby/tests/multidevices/clients/test_support/snippet_helper/tests/AndroidManifest.xml
new file mode 100644
index 0000000..f1fef23
--- /dev/null
+++ b/nearby/tests/multidevices/clients/test_support/snippet_helper/tests/AndroidManifest.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ ~ Copyright (C) 2022 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.
+ -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.google.android.mobly.snippet.util"/>
diff --git a/nearby/tests/multidevices/clients/test_support/snippet_helper/tests/robolectric.properties b/nearby/tests/multidevices/clients/test_support/snippet_helper/tests/robolectric.properties
new file mode 100644
index 0000000..2ea03bb
--- /dev/null
+++ b/nearby/tests/multidevices/clients/test_support/snippet_helper/tests/robolectric.properties
@@ -0,0 +1,16 @@
+#
+# Copyright (C) 2022 Google Inc.
+#
+# 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.
+#
+sdk=NEWEST_SDK
\ No newline at end of file
diff --git a/nearby/tests/multidevices/clients/tests/src/com/android/nearby/multidevices/common/SnippetEventHelperTest.kt b/nearby/tests/multidevices/clients/test_support/snippet_helper/tests/src/com/google/android/mobly/snippet/util/SnippetEventHelperTest.kt
similarity index 94%
rename from nearby/tests/multidevices/clients/tests/src/com/android/nearby/multidevices/common/SnippetEventHelperTest.kt
rename to nearby/tests/multidevices/clients/test_support/snippet_helper/tests/src/com/google/android/mobly/snippet/util/SnippetEventHelperTest.kt
index 1fbd352..641ab82 100644
--- a/nearby/tests/multidevices/clients/tests/src/com/android/nearby/multidevices/common/SnippetEventHelperTest.kt
+++ b/nearby/tests/multidevices/clients/test_support/snippet_helper/tests/src/com/google/android/mobly/snippet/util/SnippetEventHelperTest.kt
@@ -14,9 +14,8 @@
* limitations under the License.
*/
-package com.android.nearby.multidevices.common
+package com.google.android.mobly.snippet.util
-import android.nearby.multidevices.common.postSnippetEvent
import androidx.test.platform.app.InstrumentationRegistry
import com.google.android.mobly.snippet.event.EventSnippet
import com.google.android.mobly.snippet.util.Log