aboutsummaryrefslogtreecommitdiff
path: root/TrustAgent/src
diff options
context:
space:
mode:
authorRakesh Iyer <rni@google.com>2017-03-03 11:45:31 -0800
committerRakesh Iyer <rni@google.com>2017-03-07 10:12:59 -0800
commit452ad74f4c25e153c00d94c3db674deab8efb1ac (patch)
tree426d6b8c99fab1c3c5c4b1585407fbe1b64da873 /TrustAgent/src
parent8b1d4ed9d5c8b835ca46622fcd9be17549abfd14 (diff)
downloadCar-452ad74f4c25e153c00d94c3db674deab8efb1ac.tar.gz
Partial cleanup of Ble trust agent.
First pass, added licenses and log guards, will make another pass soon. Test: Manually verified. Change-Id: I31fcf1783fb4cb9c3d5617d9f31f840196ca49ac
Diffstat (limited to 'TrustAgent/src')
-rw-r--r--TrustAgent/src/com/android/car/trust/CarBleTrustAgent.java73
-rw-r--r--TrustAgent/src/com/android/car/trust/CarEnrolmentActivity.java14
-rw-r--r--TrustAgent/src/com/android/car/trust/CarEnrolmentService.java18
-rw-r--r--TrustAgent/src/com/android/car/trust/CarUnlockService.java19
-rw-r--r--TrustAgent/src/com/android/car/trust/PhoneEnrolmentActivity.java12
-rw-r--r--TrustAgent/src/com/android/car/trust/PhoneEnrolmentController.java28
-rw-r--r--TrustAgent/src/com/android/car/trust/PhoneUnlockController.java11
-rw-r--r--TrustAgent/src/com/android/car/trust/Utils.java15
-rw-r--r--TrustAgent/src/com/android/car/trust/comms/SimpleBleClient.java49
-rw-r--r--TrustAgent/src/com/android/car/trust/comms/SimpleBleServer.java32
10 files changed, 193 insertions, 78 deletions
diff --git a/TrustAgent/src/com/android/car/trust/CarBleTrustAgent.java b/TrustAgent/src/com/android/car/trust/CarBleTrustAgent.java
index cc7c1c1647..99bbf2277a 100644
--- a/TrustAgent/src/com/android/car/trust/CarBleTrustAgent.java
+++ b/TrustAgent/src/com/android/car/trust/CarBleTrustAgent.java
@@ -74,7 +74,9 @@ public class CarBleTrustAgent extends TrustAgentService {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
- Log.d(TAG, "Received broadcast: " + action);
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Received broadcast: " + action);
+ }
if (ACTION_REVOKE_TRUST.equals(action)) {
revokeTrust();
} else if (ACTION_ADD_TOKEN.equals(action)) {
@@ -93,14 +95,18 @@ public class CarBleTrustAgent extends TrustAgentService {
@Override
public void onTrustTimeout() {
super.onTrustTimeout();
- Log.d(TAG, "onTrustTimeout(): timeout expired");
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "onTrustTimeout(): timeout expired");
+ }
}
@Override
public void onCreate() {
super.onCreate();
- Log.d(TAG, "Bluetooth trust agent starting up");
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Bluetooth trust agent starting up");
+ }
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_REVOKE_TRUST);
filter.addAction(ACTION_ADD_TOKEN);
@@ -113,7 +119,9 @@ public class CarBleTrustAgent extends TrustAgentService {
// If the user is already unlocked, don't bother starting the BLE service.
UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
if (!um.isUserUnlocked()) {
- Log.d(TAG, "User locked, will now bind CarUnlockService");
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "User locked, will now bind CarUnlockService");
+ }
Intent intent = new Intent(this, CarUnlockService.class);
bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
@@ -124,7 +132,9 @@ public class CarBleTrustAgent extends TrustAgentService {
@Override
public void onDestroy() {
- Log.d(TAG, "Car Trust agent shutting down");
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Car Trust agent shutting down");
+ }
mLocalBroadcastManager.unregisterReceiver(mTrustEventReceiver);
// Unbind the service to avoid leaks from BLE stack.
@@ -138,18 +148,22 @@ public class CarBleTrustAgent extends TrustAgentService {
= new SimpleBleServer.ConnectionListener() {
@Override
public void onServerStarted() {
- Log.d(TAG, "BLE server started");
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "BLE server started");
+ }
}
@Override
public void onServerStartFailed(int errorCode) {
- Log.d(TAG, "BLE server failed to start. Error Code: " + errorCode);
+ Log.w(TAG, "BLE server failed to start. Error Code: " + errorCode);
}
@Override
public void onDeviceConnected(BluetoothDevice device) {
- Log.d(TAG, "BLE device connected. Name: " + device.getName()
- + " Address: " + device.getAddress());
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "BLE device connected. Name: " + device.getName()
+ + " Address: " + device.getAddress());
+ }
}
};
@@ -164,7 +178,9 @@ public class CarBleTrustAgent extends TrustAgentService {
private ServiceConnection mServiceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
- Log.d(TAG, "CarUnlockService connected");
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "CarUnlockService connected");
+ }
mBleServiceBound = true;
CarUnlockService.UnlockServiceBinder binder
@@ -183,7 +199,9 @@ public class CarBleTrustAgent extends TrustAgentService {
};
private void maybeStartBleUnlockService() {
- Log.d(TAG, "Trying to open a Ble GATT server");
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Trying to open a Ble GATT server");
+ }
BluetoothManager btManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
@@ -210,7 +228,9 @@ public class CarBleTrustAgent extends TrustAgentService {
}, BLE_RETRY_MS);
} else {
mGattServer.close();
- Log.d(TAG, "GATT available, starting up UnlockService");
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "GATT available, starting up UnlockService");
+ }
mCarUnlockService.start();
}
}
@@ -218,15 +238,22 @@ public class CarBleTrustAgent extends TrustAgentService {
private void unlock(byte[] token, long handle) {
UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
- Log.d(TAG, "About to unlock user. Current handle: " + handle
- + " Time: " + System.currentTimeMillis());
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "About to unlock user. Current handle: " + handle
+ + " Time: " + System.currentTimeMillis());
+ }
unlockUserWithToken(handle, token, getCurrentUserHandle());
- Log.d(TAG, "Attempted to unlock user, is user unlocked? " + um.isUserUnlocked()
- + " Time: " + System.currentTimeMillis());
+
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Attempted to unlock user, is user unlocked? " + um.isUserUnlocked()
+ + " Time: " + System.currentTimeMillis());
+ }
setManagingTrust(true);
if (um.isUserUnlocked()) {
- Log.d(TAG, getString(R.string.trust_granted_explanation));
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, getString(R.string.trust_granted_explanation));
+ }
grantTrust("Granting trust from escrow token",
TRUST_DURATION_MS, FLAG_GRANT_TRUST_DISMISS_KEYGUARD);
// Trust has been granted, disable the BLE server. This trust agent service does
@@ -237,13 +264,17 @@ public class CarBleTrustAgent extends TrustAgentService {
@Override
public void onEscrowTokenRemoved(long handle, boolean successful) {
- Log.d(TAG, "onEscrowTokenRemoved. Handle: " + handle + " successful? " + successful);
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "onEscrowTokenRemoved. Handle: " + handle + " successful? " + successful);
+ }
}
@Override
public void onEscrowTokenStateReceived(long handle, int tokenState) {
boolean isActive = tokenState == TOKEN_STATE_ACTIVE;
- Log.d(TAG, "Token handle: " + handle + " isActive: " + isActive);
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Token handle: " + handle + " isActive: " + isActive);
+ }
Intent intent = new Intent();
intent.setAction(ACTION_TOKEN_STATUS_RESULT);
@@ -254,7 +285,9 @@ public class CarBleTrustAgent extends TrustAgentService {
@Override
public void onEscrowTokenAdded(byte[] token, long handle, UserHandle user) {
- Log.d(TAG, "onEscrowTokenAdded, handle: " + handle);
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "onEscrowTokenAdded, handle: " + handle);
+ }
Intent intent = new Intent();
intent.setAction(ACTION_ADD_TOKEN_RESULT);
diff --git a/TrustAgent/src/com/android/car/trust/CarEnrolmentActivity.java b/TrustAgent/src/com/android/car/trust/CarEnrolmentActivity.java
index 175430487a..89e68fe87c 100644
--- a/TrustAgent/src/com/android/car/trust/CarEnrolmentActivity.java
+++ b/TrustAgent/src/com/android/car/trust/CarEnrolmentActivity.java
@@ -69,7 +69,10 @@ public class CarEnrolmentActivity extends Activity {
intent.getPackage();
String action = intent.getAction();
- Log.d(TAG, "Received broadcast: " + action);
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Received broadcast: " + action);
+ }
+
if (ACTION_TOKEN_STATUS_RESULT.equals(action)) {
boolean tokenActive = intent.getBooleanExtra(INTENT_EXTRA_TOKEN_STATUS, false);
appendOutputText("Is token active? " + tokenActive + " handle: " + mHandle);
@@ -79,9 +82,10 @@ public class CarEnrolmentActivity extends Activity {
runOnUiThread(new Runnable() {
@Override
public void run() {
- Log.d(TAG, "about to store new handle");
mPrefs.edit().putLong(SP_HANDLE_KEY, handle).apply();
- Log.d(TAG, "stored new handle");
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "stored new handle");
+ }
}
});
@@ -159,7 +163,9 @@ public class CarEnrolmentActivity extends Activity {
try {
mHandle = mPrefs.getLong(SP_HANDLE_KEY, -1);
- Log.d(TAG, "onResume, checking handle active: " + mHandle);
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "onResume, checking handle active: " + mHandle);
+ }
isTokenActive(mHandle);
} catch (RemoteException e) {
Log.e(TAG, "Error checking if token is valid");
diff --git a/TrustAgent/src/com/android/car/trust/CarEnrolmentService.java b/TrustAgent/src/com/android/car/trust/CarEnrolmentService.java
index 4dd15b6fb1..e6bbee2116 100644
--- a/TrustAgent/src/com/android/car/trust/CarEnrolmentService.java
+++ b/TrustAgent/src/com/android/car/trust/CarEnrolmentService.java
@@ -29,7 +29,7 @@ import java.util.HashSet;
import java.util.UUID;
/**
- * A service that receives escrow token enrolment requests from remote devices.
+ * A service that receives escrow token enrollment requests from remote devices.
*/
public class CarEnrolmentService extends SimpleBleServer {
private static final String TAG = "CarEnrolmentService";
@@ -55,7 +55,7 @@ public class CarEnrolmentService extends SimpleBleServer {
public void start() {
ParcelUuid uuid = new ParcelUuid(
- UUID.fromString(getString(R.string.enrolment_service_uuid)));
+ UUID.fromString(getString(R.string.enrollment_service_uuid)));
start(uuid, mEnrolmentService);
}
@@ -69,7 +69,9 @@ public class CarEnrolmentService extends SimpleBleServer {
int requestId, BluetoothGattCharacteristic characteristic,
boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
if (characteristic.getUuid().equals(mEnrolmentEscrowToken.getUuid())) {
- Log.d(TAG, "Enrolment token received, value: " + Utils.getLong(value));
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Enrolment token received, value: " + Utils.getLong(value));
+ }
for (EnrolmentCallback callback : mCallbacks) {
callback.onEnrolmentDataReceived(value);
@@ -90,7 +92,9 @@ public class CarEnrolmentService extends SimpleBleServer {
public void sendHandle(long handle, BluetoothDevice device) {
mEnrolmentTokenHandle.setValue(Utils.getBytes(handle));
- Log.d(TAG, "Sending notification for EscrowToken Handle");
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Sending notification for EscrowToken Handle");
+ }
mGattServer.notifyCharacteristicChanged(device,
mEnrolmentTokenHandle, false /* confirm */);
}
@@ -104,18 +108,18 @@ public class CarEnrolmentService extends SimpleBleServer {
// Create services and characteristics for enrolling new unlocking escrow tokens
private void setupEnrolmentService() {
mEnrolmentService = new BluetoothGattService(
- UUID.fromString(getString(R.string.enrolment_service_uuid)),
+ UUID.fromString(getString(R.string.enrollment_service_uuid)),
BluetoothGattService.SERVICE_TYPE_PRIMARY);
// Characteristic to describe the escrow token being used for unlock
mEnrolmentEscrowToken = new BluetoothGattCharacteristic(
- UUID.fromString(getString(R.string.enrolment_token_uuid)),
+ UUID.fromString(getString(R.string.enrollment_token_uuid)),
BluetoothGattCharacteristic.PROPERTY_WRITE,
BluetoothGattCharacteristic.PERMISSION_WRITE);
// Characteristic to describe the handle being used for this escrow token
mEnrolmentTokenHandle = new BluetoothGattCharacteristic(
- UUID.fromString(getString(R.string.enrolment_handle_uuid)),
+ UUID.fromString(getString(R.string.enrollment_handle_uuid)),
BluetoothGattCharacteristic.PROPERTY_NOTIFY,
BluetoothGattCharacteristic.PERMISSION_READ);
diff --git a/TrustAgent/src/com/android/car/trust/CarUnlockService.java b/TrustAgent/src/com/android/car/trust/CarUnlockService.java
index cd0774e160..0c1bc8938a 100644
--- a/TrustAgent/src/com/android/car/trust/CarUnlockService.java
+++ b/TrustAgent/src/com/android/car/trust/CarUnlockService.java
@@ -60,7 +60,9 @@ public class CarUnlockService extends SimpleBleServer {
@Override
public void onCreate() {
super.onCreate();
- Log.d(TAG, "CarUnlockService starting up, creating BLE service");
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "CarUnlockService starting up, creating BLE service");
+ }
setupUnlockService();
}
@@ -89,11 +91,15 @@ public class CarUnlockService extends SimpleBleServer {
UUID uuid = characteristic.getUuid();
if (uuid.equals(mUnlockTokenHandle.getUuid())) {
- Log.d(TAG, "Unlock handle received, value: " + Utils.getLong(value));
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Unlock handle received, value: " + Utils.getLong(value));
+ }
mCurrentHandle = Utils.getLong(value);
unlockDataReceived();
} else if (uuid.equals(mUnlockEscrowToken.getUuid())) {
- Log.d(TAG, "Unlock escrow token received, value: " + Utils.getLong(value));
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Unlock escrow token received, value: " + Utils.getLong(value));
+ }
mCurrentToken = value;
unlockDataReceived();
}
@@ -110,8 +116,11 @@ public class CarUnlockService extends SimpleBleServer {
if (mCurrentHandle == null || mCurrentToken == null) {
return;
}
- Log.d(TAG, "Handle and token both received, requesting unlock. Time: "
- + System.currentTimeMillis());
+
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Handle and token both received, requesting unlock. Time: "
+ + System.currentTimeMillis());
+ }
// Both the handle and token has been received, try to unlock the device.
diff --git a/TrustAgent/src/com/android/car/trust/PhoneEnrolmentActivity.java b/TrustAgent/src/com/android/car/trust/PhoneEnrolmentActivity.java
index 27c7f90927..dac4b7933a 100644
--- a/TrustAgent/src/com/android/car/trust/PhoneEnrolmentActivity.java
+++ b/TrustAgent/src/com/android/car/trust/PhoneEnrolmentActivity.java
@@ -24,8 +24,8 @@ import android.widget.TextView;
* Activity to allow the user to add an escrow token to a remote device.
*/
public class PhoneEnrolmentActivity extends Activity {
- private Button mScannButton;
- private Button mEnrolButton;
+ private Button mScanButton;
+ private Button mEnrollButton;
private TextView mTextOutput;
@@ -34,13 +34,13 @@ public class PhoneEnrolmentActivity extends Activity {
super.onCreate(savedInstanceState);
setContentView(R.layout.phone_client);
- mScannButton = (Button) findViewById(R.id.ble_scan_btn);
- mEnrolButton = (Button) findViewById(R.id.action_button);
- mEnrolButton.setText(getString(R.string.enrol_button));
+ mScanButton = (Button) findViewById(R.id.ble_scan_btn);
+ mEnrollButton = (Button) findViewById(R.id.action_button);
+ mEnrollButton.setText(getString(R.string.enroll_button));
mTextOutput = (TextView) findViewById(R.id.output);
PhoneEnrolmentController controller = new PhoneEnrolmentController(this /* context */);
- controller.bind(mTextOutput, mScannButton, mEnrolButton);
+ controller.bind(mTextOutput, mScanButton, mEnrollButton);
}
}
diff --git a/TrustAgent/src/com/android/car/trust/PhoneEnrolmentController.java b/TrustAgent/src/com/android/car/trust/PhoneEnrolmentController.java
index aec7043a45..b887dc4ff9 100644
--- a/TrustAgent/src/com/android/car/trust/PhoneEnrolmentController.java
+++ b/TrustAgent/src/com/android/car/trust/PhoneEnrolmentController.java
@@ -36,15 +36,15 @@ import java.util.Random;
import java.util.UUID;
/**
- * A controller that sets up a {@link SimpleBleClient} to connect to the BLE enrolment service.
- * It also binds the UI components to control the enrolment process.
+ * A controller that sets up a {@link SimpleBleClient} to connect to the BLE enrollment service.
+ * It also binds the UI components to control the enrollment process.
*/
public class PhoneEnrolmentController {
private static final String TAG = "PhoneEnrolmentCtlr";
private String mTokenHandleKey;
private String mEscrowTokenKey;
- // BLE characteristics associated with the enrolment/add escrow token service.
+ // BLE characteristics associated with the enrollment/add escrow token service.
private BluetoothGattCharacteristic mEnrolmentTokenHandle;
private BluetoothGattCharacteristic mEnrolmentEscrowToken;
@@ -67,7 +67,7 @@ public class PhoneEnrolmentController {
mClient = new SimpleBleClient(context);
mEnrolmentServiceUuid = new ParcelUuid(
- UUID.fromString(mContext.getString(R.string.enrolment_service_uuid)));
+ UUID.fromString(mContext.getString(R.string.enrollment_service_uuid)));
mClient.addCallback(mCallback /* callback */);
mHandler = new Handler(mContext.getMainLooper());
@@ -141,7 +141,10 @@ public class PhoneEnrolmentController {
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
- Log.d(TAG, "onCharacteristicChanged: " + Utils.getLong(characteristic.getValue()));
+
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "onCharacteristicChanged: " + Utils.getLong(characteristic.getValue()));
+ }
if (characteristic.getUuid().equals(mEnrolmentTokenHandle.getUuid())) {
// Store the new token handle that the BLE server is sending us. This required
@@ -155,16 +158,21 @@ public class PhoneEnrolmentController {
@Override
public void onServiceDiscovered(BluetoothGattService service) {
if (!service.getUuid().equals(mEnrolmentServiceUuid.getUuid())) {
- Log.d(TAG, "Service UUID: " + service.getUuid() + " does not match Enrolment UUID "
- + mEnrolmentServiceUuid.getUuid());
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Service UUID: " + service.getUuid()
+ + " does not match Enrolment UUID " + mEnrolmentServiceUuid.getUuid());
+ }
return;
}
- Log.d(TAG, "Enrolment Service # characteristics: " + service.getCharacteristics().size());
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Enrolment Service # characteristics: "
+ + service.getCharacteristics().size());
+ }
mEnrolmentEscrowToken
- = Utils.getCharacteristic(R.string.enrolment_token_uuid, service, mContext);
+ = Utils.getCharacteristic(R.string.enrollment_token_uuid, service, mContext);
mEnrolmentTokenHandle
- = Utils.getCharacteristic(R.string.enrolment_handle_uuid, service, mContext);
+ = Utils.getCharacteristic(R.string.enrollment_handle_uuid, service, mContext);
mClient.setCharacteristicNotification(mEnrolmentTokenHandle, true /* enable */);
appendOutputText("Enrolment BLE client successfully connected");
diff --git a/TrustAgent/src/com/android/car/trust/PhoneUnlockController.java b/TrustAgent/src/com/android/car/trust/PhoneUnlockController.java
index de31ce5521..c956333215 100644
--- a/TrustAgent/src/com/android/car/trust/PhoneUnlockController.java
+++ b/TrustAgent/src/com/android/car/trust/PhoneUnlockController.java
@@ -135,12 +135,17 @@ public class PhoneUnlockController {
@Override
public void onServiceDiscovered(BluetoothGattService service) {
if (!service.getUuid().equals(mUnlockServiceUuid.getUuid())) {
- Log.d(TAG, "Service UUID: " + service.getUuid() + " does not match Enrolment UUID "
- + mUnlockServiceUuid.getUuid());
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Service UUID: " + service.getUuid()
+ + " does not match Enrolment UUID " + mUnlockServiceUuid.getUuid());
+ }
return;
}
- Log.d(TAG, "Unlock Service # characteristics: " + service.getCharacteristics().size());
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Unlock Service # characteristics: "
+ + service.getCharacteristics().size());
+ }
mUnlockEscrowToken
= Utils.getCharacteristic(R.string.unlock_escrow_token_uiid, service, mContext);
mUnlockTokenHandle
diff --git a/TrustAgent/src/com/android/car/trust/Utils.java b/TrustAgent/src/com/android/car/trust/Utils.java
index f47a8d4361..3e9181f88d 100644
--- a/TrustAgent/src/com/android/car/trust/Utils.java
+++ b/TrustAgent/src/com/android/car/trust/Utils.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright (C) 2017 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.car.trust;
import android.bluetooth.BluetoothGattCharacteristic;
diff --git a/TrustAgent/src/com/android/car/trust/comms/SimpleBleClient.java b/TrustAgent/src/com/android/car/trust/comms/SimpleBleClient.java
index ec5e5cabc2..77bf2d3c2c 100644
--- a/TrustAgent/src/com/android/car/trust/comms/SimpleBleClient.java
+++ b/TrustAgent/src/com/android/car/trust/comms/SimpleBleClient.java
@@ -129,7 +129,9 @@ public class SimpleBleClient {
ScanSettings.Builder settings = new ScanSettings.Builder();
settings.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY);
- Log.d(TAG, "Start scanning for uuid: " + mServiceUuid.getUuid());
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Start scanning for uuid: " + mServiceUuid.getUuid());
+ }
mScanner.startScan(filters, settings.build(), mScanCallback);
Handler handler = new Handler();
@@ -137,7 +139,9 @@ public class SimpleBleClient {
@Override
public void run() {
mScanner.stopScan(mScanCallback);
- Log.d(TAG, "Stopping Scanner");
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Stopping Scanner");
+ }
}
}, SCAN_TIME_MS);
}
@@ -215,7 +219,9 @@ public class SimpleBleClient {
return;
}
- Log.d(TAG, "Executing BLE Action type: " + action.getAction());
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Executing BLE Action type: " + action.getAction());
+ }
int actionType = action.getAction();
switch (actionType) {
@@ -258,14 +264,18 @@ public class SimpleBleClient {
@Override
public void onScanResult(int callbackType, ScanResult result) {
BluetoothDevice device = result.getDevice();
- Log.d(TAG, "Scan result found: " + result.getScanRecord().getServiceUuids());
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Scan result found: " + result.getScanRecord().getServiceUuids());
+ }
if (!hasServiceUuid(result)) {
return;
}
for (ParcelUuid uuid : result.getScanRecord().getServiceUuids()) {
- Log.d(TAG, "Scan result UUID: " + uuid);
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Scan result UUID: " + uuid);
+ }
if (uuid.equals(mServiceUuid)) {
// This client only supports connecting to one service.
// Once we find one, stop scanning and open a GATT connection to the device.
@@ -279,14 +289,16 @@ public class SimpleBleClient {
@Override
public void onBatchScanResults(List<ScanResult> results) {
for (ScanResult r : results) {
- Log.d(TAG, "Batch scanResult: "
- + r.getDevice().getName() + " " + r.getDevice().getAddress());
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Batch scanResult: " + r.getDevice().getName()
+ + " " + r.getDevice().getAddress());
+ }
}
}
@Override
public void onScanFailed(int errorCode) {
- Log.d(TAG, "Scan failed: " + errorCode);
+ Log.w(TAG, "Scan failed: " + errorCode);
}
};
@@ -310,13 +322,17 @@ public class SimpleBleClient {
callback.onDeviceDisconnected();
}
}
- Log.d(TAG, " Gatt connection status: " + getStatus(status) + " newState: " + state);
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, " Gatt connection status: " + getStatus(status) + " newState: " + state);
+ }
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
- Log.d(TAG, "onServicesDiscovered: " + status);
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "onServicesDiscovered: " + status);
+ }
List<BluetoothGattService> services = gatt.getServices();
if (services == null || services.size() <= 0) {
@@ -325,7 +341,9 @@ public class SimpleBleClient {
// Notify clients of newly discovered services.
for (BluetoothGattService service : mBtGatt.getServices()) {
- Log.d(TAG, "Found service: " + service.getUuid() + " notifying clients");
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Found service: " + service.getUuid() + " notifying clients");
+ }
for (ClientCallback callback : mCallbacks) {
callback.onServiceDiscovered(service);
}
@@ -335,14 +353,18 @@ public class SimpleBleClient {
@Override
public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
- Log.d(TAG, "onCharacteristicWrite: " + status);
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "onCharacteristicWrite: " + status);
+ }
processNextAction();
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
- Log.d(TAG, "onCharacteristicRead:" + new String(characteristic.getValue()));
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "onCharacteristicRead:" + new String(characteristic.getValue()));
+ }
processNextAction();
}
@@ -355,5 +377,4 @@ public class SimpleBleClient {
processNextAction();
}
};
-
}
diff --git a/TrustAgent/src/com/android/car/trust/comms/SimpleBleServer.java b/TrustAgent/src/com/android/car/trust/comms/SimpleBleServer.java
index d55db9e165..da3f7accce 100644
--- a/TrustAgent/src/com/android/car/trust/comms/SimpleBleServer.java
+++ b/TrustAgent/src/com/android/car/trust/comms/SimpleBleServer.java
@@ -154,7 +154,9 @@ public abstract class SimpleBleServer extends Service {
}
public void addConnectionListener(ConnectionListener listener) {
- Log.d(TAG, "Adding connection listener");
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Adding connection listener");
+ }
mListeners.add(listener);
}
@@ -176,7 +178,9 @@ public abstract class SimpleBleServer extends Service {
@Override
public void onStartSuccess(AdvertiseSettings settingsInEffect) {
super.onStartSuccess(settingsInEffect);
- Log.d(TAG, "Successfully started advertising service");
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Successfully started advertising service");
+ }
for (ConnectionListener listener : mListeners) {
listener.onServerStarted();
}
@@ -185,7 +189,9 @@ public abstract class SimpleBleServer extends Service {
@Override
public void onStartFailure(int errorCode) {
super.onStartFailure(errorCode);
- Log.d(TAG, "Failed to advertise, errorCode: " + errorCode);
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Failed to advertise, errorCode: " + errorCode);
+ }
for (ConnectionListener listener : mListeners) {
listener.onServerStartFailed(errorCode);
}
@@ -196,9 +202,11 @@ public abstract class SimpleBleServer extends Service {
@Override
public void onConnectionStateChange(BluetoothDevice device,
final int status, final int newState) {
- Log.d(TAG, "GattServer connection change status: "
- + newState + " newState: "
- + newState + " device name: " + device.getName());
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "GattServer connection change status: "
+ + newState + " newState: "
+ + newState + " device name: " + device.getName());
+ }
if (newState == BluetoothProfile.STATE_CONNECTED) {
for (ConnectionListener listener : mListeners) {
listener.onDeviceConnected(device);
@@ -208,13 +216,17 @@ public abstract class SimpleBleServer extends Service {
@Override
public void onServiceAdded(final int status, BluetoothGattService service) {
- Log.d(TAG, "Service added status: " + status + " uuid: " + service.getUuid());
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Service added status: " + status + " uuid: " + service.getUuid());
+ }
}
@Override
public void onCharacteristicReadRequest(BluetoothDevice device,
int requestId, int offset, final BluetoothGattCharacteristic characteristic) {
- Log.d(TAG, "Read request for characteristic: " + characteristic.getUuid());
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Read request for characteristic: " + characteristic.getUuid());
+ }
mGattServer.sendResponse(device, requestId,
BluetoothGatt.GATT_SUCCESS, offset, characteristic.getValue());
SimpleBleServer.
@@ -225,7 +237,9 @@ public abstract class SimpleBleServer extends Service {
public void onCharacteristicWriteRequest(final BluetoothDevice device, int requestId,
BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean
responseNeeded, int offset, byte[] value) {
- Log.d(TAG, "Write request for characteristic: " + characteristic.getUuid());
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Write request for characteristic: " + characteristic.getUuid());
+ }
mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS,
offset, value);