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

Commit 0d4807d2 authored by Jack He's avatar Jack He
Browse files

Metric: Add logging for link layer connection events

* Logs when there is an event affecting Bluetooth device's link
  layer connection. Triggerred when there is a related HCI command
  or event
* Users of this metrics can deduce Bluetooth device's connection state
  from these events
* HCI commands are logged before the command is sent, after receiving,
  command status, and after receiving command complete
  event comes back
* HCI events are logged when they arrive
* Simplifies logic in btu_hcif_hdl_command_status() since p_cmd is never
  null and we always ignore parameter total length field
* Call btm_identity_addr_to_random_pseudo() when parsing command status
  packet for LE create connection, extended create connection,
  add/remove/clear whitelist commands

DETAILS:

* Bluetooth classic commands:
  - CMD_CREATE_CONNECTION
  - CMD_DISCONNECT
  - CMD_CREATE_CONNECTION_CANCEL
  - CMD_ACCEPT_CONNECTION_REQUEST
  - CMD_REJECT_CONNECTION_REQUEST
  - CMD_SETUP_ESCO_CONNECTION
  - CMD_ACCEPT_ESCO_CONNECTION
  - CMD_REJECT_ESCO_CONNECTION
  - CMD_ENH_SETUP_ESCO_CONNECTION
  - CMD_ENH_ACCEPT_ESCO_CONNECTION
* Bluetooth low energy commands:
  - CMD_BLE_CREATE_LL_CONN [Only logged when there is an error or initiator filter policy is 0x00]
  - CMD_BLE_CREATE_CONN_CANCEL [Only logged when there is an error]
  - CMD_BLE_EXTENDED_CREATE_CONNECTION [Only logged on error or when initiator filter policy is 0x00]
  - CMD_BLE_CLEAR_WHITE_LIST
  - CMD_BLE_ADD_WHITE_LIST
  - CMD_BLE_REMOVE_WHITE_LIST
* Bluetooth classic events:
  - EVT_CONNECTION_COMP
  - EVT_CONNECTION_REQUEST
  - EVT_DISCONNECTION_COMP
  - EVT_ESCO_CONNECTION_COMP
  - EVT_ESCO_CONNECTION_CHANGED
* Bluetooth low energy meta events:
  - BLE_EVT_CONN_COMPLETE_EVT
  - BLE_EVT_ENHANCED_CONN_COMPLETE_EVT

Bug: 112969790
Test: make and test drive statsd
Change-Id: Ib843dfa95bb6448c41dac261dabcf17947efda06
parent 1fdcae14
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -62,8 +62,10 @@ fluoride_defaults {
    },
    defaults: ["fluoride_types_defaults"],
    header_libs: ["libbluetooth_headers"],
    shared_libs: ["libstatslog"],
    static_libs: [
        "libbluetooth-types",
        "libbt-platform-protos-lite",
    ],
    cpp_std: "c++17",
    sanitize: {
+30 −0
Original line number Diff line number Diff line
@@ -29,11 +29,13 @@
#include <base/base64.h>
#include <base/logging.h>
#include <include/hardware/bt_av.h>
#include <statslog.h>

#include "bluetooth/metrics/bluetooth.pb.h"
#include "osi/include/osi.h"
#include "stack/include/btm_api_types.h"

#include "address_obfuscator.h"
#include "leaky_bonded_queue.h"
#include "metrics.h"
#include "time_util.h"
@@ -567,6 +569,34 @@ void BluetoothMetricsLogger::Reset() {
  pimpl_->scan_event_queue_->Clear();
}

void LogLinkLayerConnectionEvent(const RawAddress* address,
                                 uint32_t connection_handle,
                                 android::bluetooth::DirectionEnum direction,
                                 uint32_t link_type, uint32_t hci_cmd,
                                 uint32_t hci_event, uint32_t hci_ble_event,
                                 uint32_t cmd_status, uint32_t reason_code) {
  std::string obfuscated_id;
  if (address != nullptr) {
    obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(*address);
  }
  // nullptr and size 0 represent missing value for obfuscated_id
  android::util::BytesField bytes_field(
      address != nullptr ? obfuscated_id.c_str() : nullptr,
      address != nullptr ? obfuscated_id.size() : 0);
  int ret = android::util::stats_write(
      android::util::BLUETOOTH_LINK_LAYER_CONNECTION_EVENT, bytes_field,
      connection_handle, direction, link_type, hci_cmd, hci_event,
      hci_ble_event, cmd_status, reason_code);
  if (ret < 0) {
    LOG(WARNING) << __func__ << ": failed to log status 0x"
                 << loghex(cmd_status) << ", reason 0x" << loghex(reason_code)
                 << " from cmd 0x" << loghex(hci_cmd) << ", event 0x"
                 << loghex(hci_event) << ", ble_event 0x"
                 << loghex(hci_ble_event) << " for " << address << ", handle "
                 << connection_handle << ", error " << ret;
  }
}

}  // namespace common

}  // namespace bluetooth
+28 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
#pragma once

#include <bta/include/bta_api.h>
#include <frameworks/base/core/proto/android/bluetooth/enums.pb.h>
#include <stdint.h>
#include <memory>
#include <string>
@@ -276,6 +277,33 @@ class BluetoothMetricsLogger {
  std::unique_ptr<impl> const pimpl_;
};

/**
 * Unknown connection handle for metrics purpose
 */
static const uint32_t kUnknownConnectionHandle = 0xFFFF;

/**
 * Log link layer connection event
 *
 * @param address Stack wide consistent Bluetooth address of this event,
 *                nullptr if unknown
 * @param connection_handle connection handle of this event,
 *                          {@link kUnknownConnectionHandle} if unknown
 * @param direction direction of this connection
 * @param link_type type of the link
 * @param hci_cmd HCI command opecode associated with this event, if any
 * @param hci_event HCI event code associated with this event, if any
 * @param hci_ble_event HCI BLE event code associated with this event, if any
 * @param cmd_status Command status associated with this event, if any
 * @param reason_code Reason code associated with this event, if any
 */
void LogLinkLayerConnectionEvent(const RawAddress* address,
                                 uint32_t connection_handle,
                                 android::bluetooth::DirectionEnum direction,
                                 uint32_t link_type, uint32_t hci_cmd,
                                 uint32_t hci_event, uint32_t hci_ble_event,
                                 uint32_t cmd_status, uint32_t reason_code);

}  // namespace common

}  // namespace bluetooth
+1 −0
Original line number Diff line number Diff line
@@ -115,6 +115,7 @@ cc_test {
        "liblog",
        "libprotobuf-cpp-lite",
        "libcutils",
        "libcrypto",
    ],
    static_libs: [
        "libbt-common",
+1 −0
Original line number Diff line number Diff line
@@ -267,6 +267,7 @@ cc_test {
    shared_libs: [
        "libcutils",
        "libprotobuf-cpp-lite",
        "libcrypto",
    ],
    static_libs: [
        "liblog",
Loading