Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 30142936 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes I11ce2285,I03e6b096 into sc-v2-dev

* changes:
  LE_SCAN: Add runtime verbose logging for LE scan result parsing
  LE_SCAN: Add more information to scanner dumpsys
parents 06dc201f 552bdfc5
Loading
Loading
Loading
Loading
+9 −2
Original line number Diff line number Diff line
@@ -125,10 +125,10 @@ import com.android.internal.os.BackgroundThread;
import com.android.internal.os.BinderCallsStats;
import com.android.internal.util.ArrayUtils;

import libcore.util.SneakyThrow;

import com.google.protobuf.InvalidProtocolBufferException;

import libcore.util.SneakyThrow;

import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -3668,6 +3668,7 @@ public class AdapterService extends Service {
        if (DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_BLUETOOTH,
                LOGGING_DEBUG_ENABLED_FOR_ALL_FLAG, false)) {
            initFlags.add(String.format("%s=%s", LOGGING_DEBUG_ENABLED_FOR_ALL_FLAG, "true"));
            mIsVerboseLoggingEnabledForAll = true;
        }
        String debugLoggingEnabledTags = DeviceConfig.getString(DeviceConfig.NAMESPACE_BLUETOOTH,
                LOGGING_DEBUG_ENABLED_FOR_TAGS_FLAG, "");
@@ -3687,6 +3688,12 @@ public class AdapterService extends Service {
        return initFlags.toArray(new String[0]);
    }

    private boolean mIsVerboseLoggingEnabledForAll = false;

    public boolean getIsVerboseLoggingEnabledForAll() {
        return mIsVerboseLoggingEnabledForAll;
    }

    private final Object mDeviceConfigLock = new Object();

    /**
+98 −6
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
 */
package com.android.bluetooth.gatt;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanSettings;
import android.os.Binder;
@@ -75,14 +76,21 @@ import java.util.Objects;
        public boolean isFilterScan;
        public boolean isCallbackScan;
        public boolean isBatchScan;
        public boolean isLegacy;
        public int results;
        public int scannerId;
        public int scanMode;
        public int scanCallbackType;
        public int phy;
        public int scanResultType;
        public long reportDelayMillis;
        public int numOfMatchesPerFilter;
        public int matchMode;
        public String filterString;

        LastScan(long timestamp, boolean isFilterScan, boolean isCallbackScan, int scannerId,
                int scanMode, int scanCallbackType) {
        LastScan(long timestamp, boolean isFilterScan, boolean isCallbackScan, boolean isLegacy,
                int scannerId, int scanMode, int scanCallbackType, int phy, int scanResultType,
                long reportDelayMillis, int numOfMatchesPerFilter, int matchMode) {
            this.duration = 0;
            this.timestamp = timestamp;
            this.isOpportunisticScan = false;
@@ -90,9 +98,15 @@ import java.util.Objects;
            this.isBackgroundScan = false;
            this.isFilterScan = isFilterScan;
            this.isCallbackScan = isCallbackScan;
            this.isLegacy = isLegacy;
            this.isBatchScan = false;
            this.scanMode = scanMode;
            this.scanCallbackType = scanCallbackType;
            this.phy = phy;
            this.scanResultType = scanResultType;
            this.reportDelayMillis = reportDelayMillis;
            this.numOfMatchesPerFilter = numOfMatchesPerFilter;
            this.matchMode = matchMode;
            this.results = 0;
            this.scannerId = scannerId;
            this.suspendDuration = 0;
@@ -195,8 +209,10 @@ import java.util.Objects;
        this.mScansStarted++;
        startTime = SystemClock.elapsedRealtime();

        LastScan scan = new LastScan(startTime, isFilterScan, isCallbackScan, scannerId,
                settings.getScanMode(), settings.getCallbackType());
        LastScan scan = new LastScan(startTime, isFilterScan, isCallbackScan, settings.getLegacy(),
                scannerId, settings.getScanMode(), settings.getCallbackType(), settings.getPhy(),
                settings.getScanResultType(), settings.getReportDelayMillis(),
                settings.getNumOfMatches(), settings.getMatchMode());
        if (settings != null) {
            scan.isOpportunisticScan = scan.scanMode == ScanSettings.SCAN_MODE_OPPORTUNISTIC;
            scan.isBackgroundScan =
@@ -396,6 +412,16 @@ import java.util.Objects;
        }
        if (filter.getDeviceAddress() != null) {
            filterString += " DeviceAddress=" + filter.getDeviceAddress();
            filterString += " AddressType="
                    + addressTypeToString(filter.getDeviceAddress(), filter.getAddressType());
            if (filter.getIrk() != null) {
                if (filter.getIrk().length == 0) {
                    filterString += "irkLength=0";
                } else {
                    filterString += "irkLength=" + filter.getIrk().length;
                    filterString += "irkFirstByte=" + String.format("%02x", filter.getIrk()[0]);
                }
            }
        }
        if (filter.getServiceUuid() != null) {
            filterString += " ServiceUuid=" + filter.getServiceUuid();
@@ -433,6 +459,25 @@ import java.util.Objects;
        return filterString;
    }

    private static String addressTypeToString(String address, int addressType) {
        switch (addressType) {
            case BluetoothDevice.ADDRESS_TYPE_PUBLIC:
                return "PUBLIC";
            case BluetoothDevice.ADDRESS_TYPE_RANDOM:
                int msb = Integer.parseInt(address.split(":")[0], 16);
                if ((msb & 0xC0) == 0xC0) {
                    return "RANDOM_STATIC";
                } else if ((msb & 0xC0) == 0x40) {
                    return "RANDOM_RESOLVABLE";
                } else if ((msb & 0xC0) == 0x00) {
                    return "RANDOM_NON_RESOLVABLE";
                } else {
                    return "RANDOM_INVALID[msb=0x" + String.format("%02x", msb) + "]";
                }
            default:
                return "INVALID[" + addressType + "]";
        }
    }

    private static String scanModeToString(int scanMode) {
        switch (scanMode) {
@@ -466,6 +511,43 @@ import java.util.Objects;
        }
    }

    private static String phyToString(int phy) {
        switch (phy) {
            case BluetoothDevice.PHY_LE_1M:
                return "LE_1M";
            case BluetoothDevice.PHY_LE_2M:
                return "LE_2M";
            case BluetoothDevice.PHY_LE_CODED:
                return "LE_CODED";
            case ScanSettings.PHY_LE_ALL_SUPPORTED:
                return "ALL_SUPPORTED";
            default:
                return "UNKNOWN[" + phy + "]";
        }
    }

    private static String scanResultTypeToString(int scanResultType) {
        switch (scanResultType) {
            case ScanSettings.SCAN_RESULT_TYPE_FULL:
                return "FULL";
            case ScanSettings.SCAN_RESULT_TYPE_ABBREVIATED:
                return "ABBREVIATED";
            default:
                return "UNKNOWN[" + scanResultType + "]";
        }
    }

    private static String matchModeToString(int matchMode) {
        switch (matchMode) {
            case ScanSettings.MATCH_MODE_STICKY:
                return "STICKY";
            case ScanSettings.MATCH_MODE_AGGRESSIVE:
                return "AGGRESSIVE";
            default:
                return "UNKNOWN[" + matchMode + "]";
        }
    }

    synchronized void dumpToString(StringBuilder sb) {
        long currentTime = System.currentTimeMillis();
        long currTime = SystemClock.elapsedRealtime();
@@ -583,7 +665,12 @@ import java.util.Objects;
                }
                sb.append("\n      └ " + "Scan Config: [ ScanMode="
                        + scanModeToString(scan.scanMode) + ", callbackType="
                        + callbackTypeToString(scan.scanCallbackType) + " ]");
                        + callbackTypeToString(scan.scanCallbackType) + ", isLegacy="
                        + scan.isLegacy + " phy=" + phyToString(scan.phy) + ", scanResultType="
                        + scanResultTypeToString(scan.scanResultType) + ", reportDelayMillis="
                        + scan.reportDelayMillis + ", numOfMatchesPerFilter="
                        + scan.numOfMatchesPerFilter + ", matchMode="
                        + matchModeToString(scan.matchMode) + " ]");
                if (scan.isFilterScan) {
                    sb.append(scan.filterString);
                }
@@ -633,7 +720,12 @@ import java.util.Objects;
                }
                sb.append("\n      └ " + "Scan Config: [ ScanMode="
                        + scanModeToString(scan.scanMode) + ", callbackType="
                        + callbackTypeToString(scan.scanCallbackType) + " ]");
                        + callbackTypeToString(scan.scanCallbackType) + ", isLegacy="
                        + scan.isLegacy + " phy=" + phyToString(scan.phy) + ", scanResultType="
                        + scanResultTypeToString(scan.scanResultType) + ", reportDelayMillis="
                        + scan.reportDelayMillis + ", numOfMatchesPerFilter="
                        + scan.numOfMatchesPerFilter + ", matchMode="
                        + matchModeToString(scan.matchMode) + " ]");
                if (scan.isFilterScan) {
                    sb.append(scan.filterString);
                }
+25 −3
Original line number Diff line number Diff line
@@ -1144,7 +1144,7 @@ public class GattService extends ProfileService {
    void onScanResultInternal(int eventType, int addressType, String address, int primaryPhy,
            int secondaryPhy, int advertisingSid, int txPower, int rssi, int periodicAdvInt,
            byte[] advData) {
        if (VDBG) {
        if (VDBG || mAdapterService.getIsVerboseLoggingEnabledForAll()) {
            Log.d(TAG, "onScanResult() - eventType=0x" + Integer.toHexString(eventType)
                    + ", addressType=" + addressType + ", address=" + address + ", primaryPhy="
                    + primaryPhy + ", secondaryPhy=" + secondaryPhy + ", advertisingSid=0x"
@@ -1157,6 +1157,9 @@ public class GattService extends ProfileService {
        for (ScanClient client : mScanManager.getRegularScanQueue()) {
            ScannerMap.App app = mScannerMap.getById(client.scannerId);
            if (app == null) {
                if (VDBG || mAdapterService.getIsVerboseLoggingEnabledForAll()) {
                    Log.d(TAG, "App is null for scanner ID " + client.scannerId);
                }
                continue;
            }

@@ -1168,6 +1171,8 @@ public class GattService extends ProfileService {
            if (settings.getLegacy()) {
                if ((eventType & ET_LEGACY_MASK) == 0) {
                    // If this is legacy scan, but nonlegacy result - skip.
                    Log.i(TAG, "Non legacy result in legacy scan, skipping scanner id "
                               + client.scannerId + ", eventType=" + eventType);
                    continue;
                } else {
                    // Some apps are used to fixed-size advertise data.
@@ -1185,6 +1190,10 @@ public class GattService extends ProfileService {

            if (client.hasDisavowedLocation) {
                if (mLocationDenylistPredicate.test(result)) {
                    if (VDBG || mAdapterService.getIsVerboseLoggingEnabledForAll()) {
                        Log.d(TAG, "Result in location deny list, skipping scanner id "
                                + client.scannerId);
                    }
                    continue;
                }
            }
@@ -1205,11 +1214,24 @@ public class GattService extends ProfileService {
                    result = sanitized;
                }
            }
            if (!hasPermission || !matchesFilters(client, result)) {
            if (!hasPermission) {
                if (VDBG || mAdapterService.getIsVerboseLoggingEnabledForAll()) {
                    Log.d(TAG, "scanner id " + client.scannerId + " has no result permission");
                }
                continue;
            }
            if (!matchesFilters(client, result)) {
                if (VDBG || mAdapterService.getIsVerboseLoggingEnabledForAll()) {
                    Log.d(TAG, "result did not match filter for scanner id " + client.scannerId);
                }
                continue;
            }

            if ((settings.getCallbackType() & ScanSettings.CALLBACK_TYPE_ALL_MATCHES) == 0) {
                if (VDBG || mAdapterService.getIsVerboseLoggingEnabledForAll()) {
                    Log.d(TAG, "callback type " + settings.getCallbackType()
                            + " is not ALL_MATCHES for scanner id " + client.scannerId);
                }
                continue;
            }

@@ -1225,7 +1247,7 @@ public class GattService extends ProfileService {
                            ScanSettings.CALLBACK_TYPE_ALL_MATCHES);
                }
            } catch (RemoteException | PendingIntent.CanceledException e) {
                Log.e(TAG, "Exception: " + e);
                Log.e(TAG, "Stop scan for scanner id " + client.scannerId + " due to : " + e);
                mScannerMap.remove(client.scannerId);
                mScanManager.stopScan(client.scannerId);
            }