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

Commit 4eabb4bb authored by Omair Kamil's avatar Omair Kamil
Browse files

Implement basic metrics for GATT.

More info at go/le-conn-metrics.

Bug: 372115712
Test: m Bluetooth, atest BumbleBluetoothTests
Flag: EXEMPT, metrics only
Change-Id: I9a5f8aaad1b1d3861e236af053926f40a6ef624a
parent 811f3370
Loading
Loading
Loading
Loading
+60 −1
Original line number Diff line number Diff line
@@ -1334,6 +1334,13 @@ public class GattService extends ProfileService {
        if (app != null) {
            app.callback.onClientConnectionState(
                    status, clientIf, (status == BluetoothGatt.GATT_SUCCESS), address);
            MetricsLogger.getInstance()
                    .logBluetoothEvent(
                            getDevice(address),
                            BluetoothStatsLog
                                    .BLUETOOTH_CROSS_LAYER_EVENT_REPORTED__EVENT_TYPE__GATT_CONNECT_JAVA,
                            connectionStatusToState(status),
                            app.appUid);
        }
        statsLogGattConnectionStateChange(
                BluetoothProfile.GATT, address, clientIf, connectionState, status);
@@ -1379,6 +1386,13 @@ public class GattService extends ProfileService {

        if (app != null) {
            app.callback.onClientConnectionState(status, clientIf, false, address);
            MetricsLogger.getInstance()
                    .logBluetoothEvent(
                            getDevice(address),
                            BluetoothStatsLog
                                    .BLUETOOTH_CROSS_LAYER_EVENT_REPORTED__EVENT_TYPE__GATT_DISCONNECT_JAVA,
                            BluetoothStatsLog.BLUETOOTH_CROSS_LAYER_EVENT_REPORTED__STATE__SUCCESS,
                            app.appUid);
        }
        statsLogGattConnectionStateChange(
                BluetoothProfile.GATT,
@@ -1930,7 +1944,6 @@ public class GattService extends ProfileService {
        for (Map.Entry<Integer, String> entry : connMap.entrySet()) {
            Log.d(TAG, "disconnecting addr:" + entry.getValue());
            clientDisconnect(entry.getKey(), entry.getValue(), attributionSource);
            // clientDisconnect(int clientIf, String address)
        }
    }

@@ -2156,6 +2169,15 @@ public class GattService extends ProfileService {
        }

        Log.d(TAG, "unregisterClient() - clientIf=" + clientIf);
        for (ContextMap.Connection conn : mClientMap.getConnectionByApp(clientIf)) {
            MetricsLogger.getInstance()
                    .logBluetoothEvent(
                            getDevice(conn.address),
                            BluetoothStatsLog
                                    .BLUETOOTH_CROSS_LAYER_EVENT_REPORTED__EVENT_TYPE__GATT_DISCONNECT_JAVA,
                            BluetoothStatsLog.BLUETOOTH_CROSS_LAYER_EVENT_REPORTED__STATE__END,
                            attributionSource.getUid());
        }
        mClientMap.remove(clientIf);
        mNativeInterface.gattClientUnregisterApp(clientIf);
    }
@@ -2198,6 +2220,18 @@ public class GattService extends ProfileService {
                BluetoothProtoEnums.CONNECTION_STATE_CONNECTING,
                -1);

        MetricsLogger.getInstance()
                .logBluetoothEvent(
                        getDevice(address),
                        BluetoothStatsLog
                                .BLUETOOTH_CROSS_LAYER_EVENT_REPORTED__EVENT_TYPE__GATT_CONNECT_JAVA,
                        isDirect
                                ? BluetoothStatsLog
                                        .BLUETOOTH_CROSS_LAYER_EVENT_REPORTED__STATE__DIRECT_CONNECT
                                : BluetoothStatsLog
                                        .BLUETOOTH_CROSS_LAYER_EVENT_REPORTED__STATE__INDIRECT_CONNECT,
                        attributionSource.getUid());

        int preferredMtu = 0;

        // Some applications expect MTU to be exchanged immediately on connections
@@ -2248,6 +2282,13 @@ public class GattService extends ProfileService {
                clientIf,
                BluetoothProtoEnums.CONNECTION_STATE_DISCONNECTING,
                -1);
        MetricsLogger.getInstance()
                .logBluetoothEvent(
                        getDevice(address),
                        BluetoothStatsLog
                                .BLUETOOTH_CROSS_LAYER_EVENT_REPORTED__EVENT_TYPE__GATT_DISCONNECT_JAVA,
                        BluetoothStatsLog.BLUETOOTH_CROSS_LAYER_EVENT_REPORTED__STATE__START,
                        attributionSource.getUid());
        mNativeInterface.gattClientDisconnect(clientIf, address, connId != null ? connId : 0);
    }

@@ -3797,6 +3838,24 @@ public class GattService extends ProfileService {
        mTransitionalScanHelper.dumpProto(builder);
    }

    private BluetoothDevice getDevice(String address) {
        byte[] addressBytes = Utils.getBytesFromAddress(address);
        return mAdapterService.getDeviceFromByte(addressBytes);
    }

    private static int connectionStatusToState(int status) {
        return switch (status) {
                // GATT_SUCCESS
            case 0x00 -> BluetoothStatsLog.BLUETOOTH_CROSS_LAYER_EVENT_REPORTED__STATE__SUCCESS;
                // GATT_CONNECTION_TIMEOUT
            case 0x93 ->
                    BluetoothStatsLog
                            .BLUETOOTH_CROSS_LAYER_EVENT_REPORTED__STATE__CONNECTION_TIMEOUT;
                // For now all other errors are bucketed together.
            default -> BluetoothStatsLog.BLUETOOTH_CROSS_LAYER_EVENT_REPORTED__STATE__FAIL;
        };
    }

    /**************************************************************************
     * GATT Test functions
     *************************************************************************/
+1 −0
Original line number Diff line number Diff line
@@ -428,6 +428,7 @@ cc_test {
        "libbluetooth_core_rs_bridge",
        "libbluetooth_crypto_toolbox",
        "libbluetooth_gd",
        "libbluetooth_hci_pdl",
        "libbluetooth_log",
        "libbt-audio-hal-interface",
        "libbt-bta",
+2 −0
Original line number Diff line number Diff line
@@ -503,6 +503,7 @@ cc_test {
    static_libs: [
        "libbluetooth-types",
        "libbluetooth_core_rs_bridge",
        "libbluetooth_hci_pdl",
        "libbluetooth_log",
        "libbt-platform-protos-lite",
        "libchrome",
@@ -590,6 +591,7 @@ cc_test {
        "libbluetooth_core_rs_bridge",
        "libbluetooth_crypto_toolbox",
        "libbluetooth_gd",
        "libbluetooth_hci_pdl",
        "libbluetooth_log",
        "libbt-audio-hal-interface",
        "libbt-common",
+13 −0
Original line number Diff line number Diff line
@@ -249,7 +249,9 @@ cc_binary {
        ":BluetoothFacade_hci_hal",
        ":BluetoothFacade_hci_layer",
        ":BluetoothFacade_neighbor",
        ":TestCommonMockFunctions",
        ":TestMockMainShimStack",
        ":TestMockStackMetrics",
        "facade/facade_main.cc",
        "facade/grpc_root_server.cc",
        "facade/read_only_property_server.cc",
@@ -275,6 +277,7 @@ cc_binary {
        "libbluetooth_ras_pdl",
        "libbluetooth_smp_pdl",
        "libbt-common",
        "libbt-platform-protos-lite",
        "libchrome",
        "libcom.android.sysprop.bluetooth.wrapped",
        "libflatbuffers-cpp",
@@ -420,6 +423,8 @@ cc_test {
        ":BluetoothOsTestSources",
        ":BluetoothPacketTestSources",
        ":BluetoothStorageUnitTestSources",
        ":TestCommonMockFunctions",
        ":TestMockStackMetrics",
        "module_unittest.cc",
        "stack_manager_unittest.cc",
    ],
@@ -581,8 +586,16 @@ cc_fuzz {
    name: "bluetooth_gd_acl_manager_fuzz_test",
    defaults: ["gd_fuzz_defaults"],
    srcs: [
        ":TestCommonMockFunctions",
        ":TestMockStackMetrics",
        "hci/fuzz/acl_manager_fuzz_test.cc",
    ],
    include_dirs: [
        "packages/modules/Bluetooth/system",
    ],
    static_libs: [
        "libbt-platform-protos-lite",
    ],
    fuzz_config: {
        cc: ["android-bluetooth-security@google.com"],
        componentid: 27441,
+6 −0
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@
#include "os/alarm.h"
#include "os/handler.h"
#include "os/system_properties.h"
#include "stack/include/stack_metrics_logging.h"

namespace bluetooth {
namespace hci {
@@ -404,6 +405,8 @@ public:
      return;
    }

    log_le_connection_status(address, true /* is_connect */, status);

    const bool in_filter_accept_list = is_device_in_accept_list(remote_address);

    if (role == hci::Role::CENTRAL) {
@@ -560,6 +563,7 @@ public:
      arm_on_resume_ = true;
      add_device_to_accept_list(remote_address);
    }
    log_le_connection_status(remote_address.GetAddress(), false /* is_connect */, reason);
  }

  void on_le_connection_update_complete(LeMetaEventView view) {
@@ -697,6 +701,7 @@ public:
  }

  void add_device_to_accept_list(AddressWithType address_with_type) {
    log_le_device_in_accept_list(address_with_type.GetAddress(), true /* is_add */);
    if (connections.alreadyConnected(address_with_type)) {
      log::info("Device already connected, return");
      return;
@@ -719,6 +724,7 @@ public:
  }

  void remove_device_from_accept_list(AddressWithType address_with_type) {
    log_le_device_in_accept_list(address_with_type.GetAddress(), false /* is_add */);
    if (accept_list.find(address_with_type) == accept_list.end()) {
      log::warn("Device not in acceptlist and cannot be removed: {}", address_with_type);
      return;
Loading