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

Commit 7296f053 authored by Ajay Panicker's avatar Ajay Panicker Committed by Gerrit Code Review
Browse files

Merge "Add scan results to scan stats"

parents d8166b93 c30f0666
Loading
Loading
Loading
Loading
+21 −4
Original line number Diff line number Diff line
@@ -35,11 +35,15 @@ import com.android.bluetooth.btservice.BluetoothProto;
    /* ContextMap here is needed to grab Apps and Connections */
    ContextMap contextMap;

    /* GattService is needed to add scan event protos to be dumped later */
    GattService gattService;

    class LastScan {
        long duration;
        long timestamp;
        boolean opportunistic;
        boolean background;
        int results;

        public LastScan(long timestamp, long duration,
                        boolean opportunistic, boolean background) {
@@ -47,6 +51,7 @@ import com.android.bluetooth.btservice.BluetoothProto;
            this.timestamp = timestamp;
            this.opportunistic = opportunistic;
            this.background = background;
            this.results = 0;
        }
    }

@@ -63,10 +68,19 @@ import com.android.bluetooth.btservice.BluetoothProto;
    List<LastScan> lastScans = new ArrayList<LastScan>(NUM_SCAN_DURATIONS_KEPT + 1);
    long startTime = 0;
    long stopTime = 0;
    int results = 0;

    public AppScanStats(String name, ContextMap map) {
    public AppScanStats(String name, ContextMap map, GattService service) {
        appName = name;
        contextMap = map;
        gattService = service;
    }

    synchronized void addResult() {
        if (!lastScans.isEmpty())
            lastScans.get(lastScans.size() - 1).results++;

        results++;
    }

    synchronized void recordScanStart(ScanSettings settings) {
@@ -89,7 +103,7 @@ import com.android.bluetooth.btservice.BluetoothProto;
        scanEvent.setScanTechnologyType(BluetoothProto.ScanEvent.SCAN_TECH_TYPE_LE);
        scanEvent.setInitiator(appName);
        scanEvent.setEventTimeMillis(System.currentTimeMillis());
        contextMap.addScanEvent(scanEvent);
        gattService.addScanEvent(scanEvent);
    }

    synchronized void recordScanStop() {
@@ -117,7 +131,7 @@ import com.android.bluetooth.btservice.BluetoothProto;
        scanEvent.setScanTechnologyType(BluetoothProto.ScanEvent.SCAN_TECH_TYPE_LE);
        scanEvent.setInitiator(appName);
        scanEvent.setEventTimeMillis(System.currentTimeMillis());
        contextMap.addScanEvent(scanEvent);
        gattService.addScanEvent(scanEvent);
    }

    synchronized void dumpToString(StringBuilder sb) {
@@ -159,6 +173,8 @@ import com.android.bluetooth.btservice.BluetoothProto;
                  maxScan + " / " +
                  avgScan + " / " +
                  totalScanTime + "\n");
        sb.append("  Total number of results            : " +
                  results + "\n");

        if (lastScans.size() != 0) {
            int lastScansSize = scansStopped < NUM_SCAN_DURATIONS_KEPT ?
@@ -173,6 +189,7 @@ import com.android.bluetooth.btservice.BluetoothProto;
                sb.append(scan.duration + "ms ");
                if (scan.opportunistic) sb.append("Opp ");
                if (scan.background) sb.append("Back ");
                sb.append(scan.results + " results");
                sb.append("\n");
            }
        }
+11 −28
Original line number Diff line number Diff line
@@ -40,7 +40,6 @@ import com.android.bluetooth.btservice.BluetoothProto;
 */
/*package*/ class ContextMap<T> {
    private static final String TAG = GattServiceConfig.TAG_PREFIX + "ContextMap";
    static final int NUM_SCAN_EVENTS_KEPT = 20;

    /**
     * Connection class helps map connection IDs to device addresses.
@@ -72,6 +71,9 @@ import com.android.bluetooth.btservice.BluetoothProto;
        /** The package name of the application */
        String name;

        /** Statistics for this app */
        AppScanStats appScanStats;

        /** Application callbacks */
        T callback;

@@ -87,10 +89,11 @@ import com.android.bluetooth.btservice.BluetoothProto;
        /**
         * Creates a new app context.
         */
        App(UUID uuid, T callback, String name) {
        App(UUID uuid, T callback, String name, AppScanStats appScanStats) {
            this.uuid = uuid;
            this.callback = callback;
            this.name = name;
            this.appScanStats = appScanStats;
        }

        /**
@@ -136,28 +139,24 @@ import com.android.bluetooth.btservice.BluetoothProto;
    /** Internal map to keep track of logging information by app name */
    HashMap<String, AppScanStats> mAppScanStats = new HashMap<String, AppScanStats>();

    /** Internal list of scan events to use with the proto */
    ArrayList<BluetoothProto.ScanEvent> mScanEvents =
        new ArrayList<BluetoothProto.ScanEvent>(NUM_SCAN_EVENTS_KEPT);

    /** Internal list of connected devices **/
    Set<Connection> mConnections = new HashSet<Connection>();

    /**
     * Add an entry to the application context list.
     */
    void add(UUID uuid, T callback, Context context) {
        String appName = context.getPackageManager().getNameForUid(
    void add(UUID uuid, T callback, GattService service) {
        String appName = service.getPackageManager().getNameForUid(
                             Binder.getCallingUid());
        if (appName == null) {
            // Assign an app name if one isn't found
            appName = "Unknown App (UID: " + Binder.getCallingUid() + ")";
        }
        synchronized (mApps) {
            mApps.add(new App(uuid, callback, appName));
            AppScanStats appScanStats = mAppScanStats.get(appName);
            mApps.add(new App(uuid, callback, appName, appScanStats));
            if (appScanStats == null) {
                appScanStats = new AppScanStats(appName, this);
                appScanStats = new AppScanStats(appName, this, service);
                mAppScanStats.put(appName, appScanStats);
            }
            appScanStats.isRegistered = true;
@@ -174,7 +173,7 @@ import com.android.bluetooth.btservice.BluetoothProto;
                App entry = i.next();
                if (entry.uuid.equals(uuid)) {
                    entry.unlinkToDeath();
                    mAppScanStats.get(entry.name).isRegistered = false;
                    entry.appScanStats.isRegistered = false;
                    i.remove();
                    break;
                }
@@ -273,7 +272,7 @@ import com.android.bluetooth.btservice.BluetoothProto;
    AppScanStats getAppScanStatsById(int id) {
        App temp = getById(id);
        if (temp != null) {
            return mAppScanStats.get(temp.name);
            return temp.appScanStats;
        }
        return null;
    }
@@ -380,14 +379,6 @@ import com.android.bluetooth.btservice.BluetoothProto;
        return connectedmap;
    }

    void addScanEvent(BluetoothProto.ScanEvent event) {
        synchronized(mScanEvents) {
            if (mScanEvents.size() == NUM_SCAN_EVENTS_KEPT)
                mScanEvents.remove(0);
            mScanEvents.add(event);
        }
    }

    /**
     * Logs debug information.
     */
@@ -403,12 +394,4 @@ import com.android.bluetooth.btservice.BluetoothProto;
            appScanStats.dumpToString(sb);
        }
    }

    void dumpProto(BluetoothProto.BluetoothLog proto) {
        synchronized(mScanEvents) {
            for (BluetoothProto.ScanEvent event : mScanEvents) {
                proto.addScanEvent(event);
            }
        }
    }
}
+31 −2
Original line number Diff line number Diff line
@@ -125,6 +125,13 @@ public class GattService extends ProfileService {
     */
    private List<ServiceDeclaration> mServiceDeclarations = new ArrayList<ServiceDeclaration>();

    static final int NUM_SCAN_EVENTS_KEPT = 20;
    /**
     * Internal list of scan events to use with the proto
     */
    ArrayList<BluetoothProto.ScanEvent> mScanEvents =
        new ArrayList<BluetoothProto.ScanEvent>(NUM_SCAN_EVENTS_KEPT);

    private ServiceDeclaration addDeclaration() {
        synchronized (mServiceDeclarations) {
            mServiceDeclarations.add(new ServiceDeclaration());
@@ -587,6 +594,8 @@ public class GattService extends ProfileService {
        if (VDBG) Log.d(TAG, "onScanResult() - address=" + address
                    + ", rssi=" + rssi);
        List<UUID> remoteUuids = parseUuids(adv_data);
        addScanResult();

        for (ScanClient client : mScanManager.getRegularScanQueue()) {
            if (client.uuids.length > 0) {
                int matches = 0;
@@ -616,6 +625,7 @@ public class GattService extends ProfileService {
                            ScanSettings settings = client.settings;
                            if ((settings.getCallbackType() &
                                    ScanSettings.CALLBACK_TYPE_ALL_MATCHES) != 0) {
                                app.appScanStats.addResult();
                                app.callback.onScanResult(result);
                            }
                        } catch (RemoteException e) {
@@ -2288,10 +2298,29 @@ public class GattService extends ProfileService {
        mHandleMap.dump(sb);
    }

    void addScanResult() {
        if (mScanEvents.isEmpty())
            return;

        BluetoothProto.ScanEvent curr = mScanEvents.get(mScanEvents.size() - 1);
        curr.setNumberResults(curr.getNumberResults() + 1);
    }

    void addScanEvent(BluetoothProto.ScanEvent event) {
        synchronized(mScanEvents) {
            if (mScanEvents.size() == NUM_SCAN_EVENTS_KEPT)
                mScanEvents.remove(0);
            mScanEvents.add(event);
        }
    }

    @Override
    public void dumpProto(BluetoothProto.BluetoothLog proto) {
        mClientMap.dumpProto(proto);
        mServerMap.dumpProto(proto);
        synchronized(mScanEvents) {
            for (BluetoothProto.ScanEvent event : mScanEvents) {
                proto.addScanEvent(event);
            }
        }
    }

    /**************************************************************************