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

Commit b75275d4 authored by Jack He's avatar Jack He
Browse files

Metrics: Log SMP pairing commands

* Log SMP pairing commands and pairing failure reasons

Bug: 112969790
Test: make, testdrive with statsd
Change-Id: I51ae2af981a687954f7de3cee3caacce43d782b6
parent c49c9b1f
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -735,6 +735,28 @@ void LogReadTxPowerLevelResult(const RawAddress& address, uint16_t handle,
  }
}

void LogSmpPairingEvent(const RawAddress& address, uint8_t smp_cmd,
                        android::bluetooth::DirectionEnum direction,
                        uint8_t smp_fail_reason) {
  std::string obfuscated_id;
  if (!address.IsEmpty()) {
    obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address);
  }
  // nullptr and size 0 represent missing value for obfuscated_id
  android::util::BytesField obfuscated_id_field(
      address.IsEmpty() ? nullptr : obfuscated_id.c_str(),
      address.IsEmpty() ? 0 : obfuscated_id.size());
  int ret = android::util::stats_write(
      android::util::BLUETOOTH_SMP_PAIRING_EVENT_REPORTED, obfuscated_id_field,
      smp_cmd, direction, smp_fail_reason);
  if (ret < 0) {
    LOG(WARNING) << __func__ << ": failed for " << address << ", smp_cmd "
                 << loghex(smp_cmd) << ", direction " << direction
                 << ", smp_fail_reason " << loghex(smp_fail_reason)
                 << ", error " << ret;
  }
}

}  // namespace common

}  // namespace bluetooth
+11 −0
Original line number Diff line number Diff line
@@ -399,6 +399,17 @@ void LogReadTxPowerLevelResult(const RawAddress& address, uint16_t handle,
                               uint32_t cmd_status,
                               int32_t transmit_power_level);

/**
 * Logs when there is an event related to Bluetooth Security Manager Protocol
 *
 * @param address address of associated device
 * @param smp_cmd SMP command code associated with this event
 * @param direction direction of this SMP command
 * @param smp_fail_reason SMP pairing failure reason code from SMP spec
 */
void LogSmpPairingEvent(const RawAddress& address, uint8_t smp_cmd,
                        android::bluetooth::DirectionEnum direction,
                        uint8_t smp_fail_reason);
}  // namespace common

}  // namespace bluetooth
+2 −0
Original line number Diff line number Diff line
@@ -440,6 +440,8 @@ extern void smp_l2cap_if_init(void);
extern void smp_data_ind(const RawAddress& bd_addr, BT_HDR* p_buf);

/* smp_util.cc */
extern void smp_log_metrics(const RawAddress& bd_addr, bool is_outgoing,
                            const uint8_t* p_buf, size_t buf_len);
extern bool smp_send_cmd(uint8_t cmd_code, tSMP_CB* p_cb);
extern void smp_cb_cleanup(tSMP_CB* p_cb);
extern void smp_reset_control_value(tSMP_CB* p_cb);
+7 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@

#include <string.h>
#include "btm_ble_api.h"
#include "common/metrics.h"
#include "l2c_api.h"

#include "smp_int.h"
@@ -172,6 +173,9 @@ static void smp_data_received(uint16_t channel, const RawAddress& bd_addr,
    alarm_set_on_mloop(p_cb->smp_rsp_timer_ent, SMP_WAIT_FOR_RSP_TIMEOUT_MS,
                       smp_rsp_timeout, NULL);

    smp_log_metrics(p_cb->pairing_bda, false /* incoming */,
                    p_buf->data + p_buf->offset, p_buf->len);

    if (cmd == SMP_OPCODE_CONFIRM) {
      SMP_TRACE_DEBUG(
          "in %s cmd = 0x%02x, peer_auth_req = 0x%02x,"
@@ -310,6 +314,9 @@ static void smp_br_data_received(uint16_t channel, const RawAddress& bd_addr,
    alarm_set_on_mloop(p_cb->smp_rsp_timer_ent, SMP_WAIT_FOR_RSP_TIMEOUT_MS,
                       smp_rsp_timeout, NULL);

    smp_log_metrics(p_cb->pairing_bda, false /* incoming */,
                    p_buf->data + p_buf->offset, p_buf->len);

    p_cb->rcvd_cmd_code = cmd;
    p_cb->rcvd_cmd_len = (uint8_t)p_buf->len;
    tSMP_INT_DATA smp_int_data;
+32 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@
#include "bt_utils.h"
#include "btm_ble_api.h"
#include "btm_int.h"
#include "common/metrics.h"
#include "device/include/controller.h"
#include "hcidefs.h"
#include "l2c_api.h"
@@ -295,6 +296,34 @@ static tSMP_ASSO_MODEL smp_select_legacy_association_model(tSMP_CB* p_cb);
static tSMP_ASSO_MODEL smp_select_association_model_secure_connections(
    tSMP_CB* p_cb);

/**
 * Log metrics data for SMP command
 *
 * @param bd_addr current pairing address
 * @param is_outgoing whether this command is outgoing
 * @param p_buf buffer to the beginning of SMP command
 * @param buf_len length available to read for p_buf
 */
void smp_log_metrics(const RawAddress& bd_addr, bool is_outgoing,
                     const uint8_t* p_buf, size_t buf_len) {
  if (buf_len < 1) {
    LOG(WARNING) << __func__ << ": buffer is too small, size is " << buf_len;
    return;
  }
  uint8_t cmd;
  STREAM_TO_UINT8(cmd, p_buf);
  buf_len--;
  uint8_t failure_reason = 0;
  if (cmd == SMP_OPCODE_PAIRING_FAILED && buf_len >= 1) {
    STREAM_TO_UINT8(failure_reason, p_buf);
  }
  android::bluetooth::DirectionEnum direction =
      is_outgoing ? android::bluetooth::DirectionEnum::DIRECTION_OUTGOING
                  : android::bluetooth::DirectionEnum::DIRECTION_INCOMING;
  bluetooth::common::LogSmpPairingEvent(bd_addr, cmd, direction,
                                        failure_reason);
}

/*******************************************************************************
 *
 * Function         smp_send_msg_to_L2CAP
@@ -313,6 +342,9 @@ bool smp_send_msg_to_L2CAP(const RawAddress& rem_bda, BT_HDR* p_toL2CAP) {
  SMP_TRACE_EVENT("%s", __func__);
  smp_cb.total_tx_unacked += 1;

  smp_log_metrics(rem_bda, true /* outgoing */,
                  p_toL2CAP->data + p_toL2CAP->offset, p_toL2CAP->len);

  l2cap_ret = L2CA_SendFixedChnlData(fixed_cid, rem_bda, p_toL2CAP);
  if (l2cap_ret == L2CAP_DW_FAILED) {
    smp_cb.total_tx_unacked -= 1;