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

Commit dd4bcaf4 authored by Henri Chataing's avatar Henri Chataing
Browse files

system/stack/smp: Migrate to libbluetooth_log

Test: m com.android.btservices
Bug: 305066880
Flag: EXEMPT, mechanical refactor
Change-Id: I3c2d741e3b0ee85ee3ec8de1a86e81e4f419dc8b
parent c2a7f599
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -232,7 +232,11 @@ typedef tBTM_STATUS(tSMP_SIRK_CALLBACK)(const RawAddress& bd_addr);

namespace fmt {
template <>
struct formatter<tSMP_OOB_DATA_TYPE> : enum_formatter<tSMP_OOB_DATA_TYPE> {};
template <>
struct formatter<tSMP_SEC_LEVEL> : enum_formatter<tSMP_SEC_LEVEL> {};
template <>
struct formatter<tSMP_EVT> : enum_formatter<tSMP_EVT> {};
}  // namespace fmt

#endif  // SMP_API_TYPES_H
+6 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
#pragma once

#include <base/strings/stringprintf.h>
#include <bluetooth/log.h>

#include <cstdint>

@@ -93,3 +94,8 @@ inline std::string smp_status_text(const tSMP_STATUS& status) {
      return base::StringPrintf("UNKNOWN[%hhu]", status);
  }
}

namespace fmt {
template <>
struct formatter<tSMP_STATUS> : enum_formatter<tSMP_STATUS> {};
}  // namespace fmt
+176 −175

File changed.

Preview size limit exceeded, changes collapsed.

+35 −33
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@

#include "smp_api.h"

#include <bluetooth/log.h>
#include <string.h>

#include "l2c_api.h"
@@ -36,6 +37,8 @@
#include "stack/include/btm_sec_api_types.h"
#include "types/raw_address.h"

using namespace bluetooth;

/*******************************************************************************
 *
 * Function         SMP_Init
@@ -57,10 +60,10 @@ void SMP_Init(uint8_t init_security_mode) { smp_cb.init(init_security_mode); }
 *
 ******************************************************************************/
bool SMP_Register(tSMP_CALLBACK* p_cback) {
  LOG_VERBOSE("state=%d", smp_cb.state);
  log::verbose("state={}", smp_cb.state);

  if (smp_cb.p_callback != NULL) {
    LOG_ERROR("duplicate registration, overwrite it");
    log::error("duplicate registration, overwrite it");
  }
  smp_cb.p_callback = p_cback;

@@ -82,7 +85,7 @@ bool SMP_Register(tSMP_CALLBACK* p_cback) {
tSMP_STATUS SMP_Pair(const RawAddress& bd_addr, tBLE_ADDR_TYPE addr_type) {
  tSMP_CB* p_cb = &smp_cb;

  LOG_VERBOSE("state=%d br_state=%d flag=0x%x, bd_addr=%s", p_cb->state,
  log::verbose("state={} br_state={} flag=0x{:x}, bd_addr={}", p_cb->state,
               p_cb->br_state, p_cb->flags, ADDRESS_TO_LOGGABLE_CSTR(bd_addr));

  if (p_cb->state != SMP_STATE_IDLE ||
@@ -101,7 +104,7 @@ tSMP_STATUS SMP_Pair(const RawAddress& bd_addr, tBLE_ADDR_TYPE addr_type) {
      tSMP_INT_DATA smp_int_data;
      smp_int_data.status = SMP_PAIR_INTERNAL_ERR;
      p_cb->status = SMP_PAIR_INTERNAL_ERR;
      LOG_ERROR("L2C connect fixed channel failed.");
      log::error("L2C connect fixed channel failed.");
      smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
      return SMP_PAIR_INTERNAL_ERR;
    }
@@ -130,7 +133,7 @@ tSMP_STATUS SMP_Pair(const RawAddress& bd_addr) {
tSMP_STATUS SMP_BR_PairWith(const RawAddress& bd_addr) {
  tSMP_CB* p_cb = &smp_cb;

  LOG_VERBOSE("state=%d br_state=%d flag=0x%x, bd_addr=%s", p_cb->state,
  log::verbose("state={} br_state={} flag=0x{:x}, bd_addr={}", p_cb->state,
               p_cb->br_state, p_cb->flags, ADDRESS_TO_LOGGABLE_CSTR(bd_addr));

  if (p_cb->state != SMP_STATE_IDLE || p_cb->smp_over_br ||
@@ -145,7 +148,7 @@ tSMP_STATUS SMP_BR_PairWith(const RawAddress& bd_addr) {
  p_cb->pairing_bda = bd_addr;

  if (!L2CA_ConnectFixedChnl(L2CAP_SMP_BR_CID, bd_addr)) {
    LOG_ERROR("L2C connect fixed channel failed.");
    log::error("L2C connect fixed channel failed.");
    tSMP_INT_DATA smp_int_data;
    smp_int_data.status = SMP_PAIR_INTERNAL_ERR;
    p_cb->status = SMP_PAIR_INTERNAL_ERR;
@@ -170,10 +173,10 @@ tSMP_STATUS SMP_BR_PairWith(const RawAddress& bd_addr) {
bool SMP_PairCancel(const RawAddress& bd_addr) {
  tSMP_CB* p_cb = &smp_cb;

  LOG_VERBOSE("state=%d flag=0x%x ", p_cb->state, p_cb->flags);
  log::verbose("state={} flag=0x{:x}", p_cb->state, p_cb->flags);
  if (p_cb->state != SMP_STATE_IDLE && p_cb->pairing_bda == bd_addr) {
    p_cb->is_pair_cancel = true;
    LOG_VERBOSE("set fail reason Unknown");
    log::verbose("set fail reason Unknown");
    tSMP_INT_DATA smp_int_data;
    smp_int_data.status = SMP_PAIR_FAIL_UNKNOWN;
    smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
@@ -197,7 +200,7 @@ bool SMP_PairCancel(const RawAddress& bd_addr) {
 *
 ******************************************************************************/
void SMP_SecurityGrant(const RawAddress& bd_addr, tSMP_STATUS res) {
  LOG_VERBOSE("addr:%s", ADDRESS_TO_LOGGABLE_CSTR(bd_addr));
  log::verbose("addr:{}", ADDRESS_TO_LOGGABLE_CSTR(bd_addr));

  // If just showing consent dialog, send response
  if (smp_cb.cb_evt == SMP_CONSENT_REQ_EVT) {
@@ -206,7 +209,7 @@ void SMP_SecurityGrant(const RawAddress& bd_addr, tSMP_STATUS res) {
      if (res == SMP_SUCCESS) {
        smp_sm_event(&smp_cb, SMP_SC_NC_OK_EVT, NULL);
      } else {
        LOG_WARN("Consent dialog fails for JUSTWORKS");
        log::warn("Consent dialog fails for JUSTWORKS");
        /* send pairing failure */
        tSMP_INT_DATA smp_int_data;
        smp_int_data.status = SMP_NUMERIC_COMPAR_FAIL;
@@ -225,7 +228,7 @@ void SMP_SecurityGrant(const RawAddress& bd_addr, tSMP_STATUS res) {
        smp_cb.tk = {0};
        smp_sm_event(&smp_cb, SMP_KEY_READY_EVT, &smp_int_data);
      } else {
        LOG_WARN("Consent dialog fails for ENCRYPTION_ONLY");
        log::warn("Consent dialog fails for ENCRYPTION_ONLY");
        /* send pairing failure */
        tSMP_INT_DATA smp_int_data;
        smp_int_data.status = SMP_NUMERIC_COMPAR_FAIL;
@@ -281,21 +284,21 @@ void SMP_PasskeyReply(const RawAddress& bd_addr, uint8_t res,
                      uint32_t passkey) {
  tSMP_CB* p_cb = &smp_cb;

  LOG_VERBOSE("Key:%d  Result:%d", passkey, res);
  log::verbose("Key:{}  Result:{}", passkey, res);

  /* If timeout already expired or has been canceled, ignore the reply */
  if (p_cb->cb_evt != SMP_PASSKEY_REQ_EVT) {
    LOG_WARN("Wrong State:%d", p_cb->state);
    log::warn("Wrong State:{}", p_cb->state);
    return;
  }

  if (bd_addr != p_cb->pairing_bda) {
    LOG_ERROR("Wrong BD Addr");
    log::error("Wrong BD Addr");
    return;
  }

  if (passkey > BTM_MAX_PASSKEY_VAL || res != SMP_SUCCESS) {
    LOG_WARN("Invalid passkey value:%d or passkey entry fail", passkey);
    log::warn("Invalid passkey value:{} or passkey entry fail", passkey);
    /* send pairing failure */
    tSMP_INT_DATA smp_int_data;
    smp_int_data.status = SMP_PASSKEY_ENTRY_FAIL;
@@ -325,21 +328,21 @@ void SMP_PasskeyReply(const RawAddress& bd_addr, uint8_t res,
void SMP_ConfirmReply(const RawAddress& bd_addr, uint8_t res) {
  tSMP_CB* p_cb = &smp_cb;

  LOG_VERBOSE("addr:%s, Result:%d", ADDRESS_TO_LOGGABLE_CSTR(bd_addr), res);
  log::verbose("addr:{}, Result:{}", ADDRESS_TO_LOGGABLE_CSTR(bd_addr), res);

  /* If timeout already expired or has been canceled, ignore the reply */
  if (p_cb->cb_evt != SMP_NC_REQ_EVT) {
    LOG_WARN("Wrong State:%d", p_cb->state);
    log::warn("Wrong State:{}", p_cb->state);
    return;
  }

  if (bd_addr != p_cb->pairing_bda) {
    LOG_ERROR("Wrong BD Addr");
    log::error("Wrong BD Addr");
    return;
  }

  if (res != SMP_SUCCESS) {
    LOG_WARN("Numeric Comparison fails");
    log::warn("Numeric Comparison fails");
    /* send pairing failure */
    tSMP_INT_DATA smp_int_data;
    smp_int_data.status = SMP_NUMERIC_COMPAR_FAIL;
@@ -366,7 +369,7 @@ void SMP_OobDataReply(const RawAddress& bd_addr, tSMP_STATUS res, uint8_t len,
  tSMP_CB* p_cb = &smp_cb;
  tSMP_KEY key;

  LOG_VERBOSE("State:%d  res:%d", smp_cb.state, res);
  log::verbose("State:{}  res:{}", smp_cb.state, res);

  /* If timeout already expired or has been canceled, ignore the reply */
  if (p_cb->state != SMP_STATE_WAIT_APP_RSP || p_cb->cb_evt != SMP_OOB_REQ_EVT)
@@ -405,16 +408,15 @@ void SMP_SecureConnectionOobDataReply(uint8_t* p_data) {

  tSMP_SC_OOB_DATA* p_oob = (tSMP_SC_OOB_DATA*)p_data;
  if (!p_oob) {
    LOG_ERROR("received no data");
    log::error("received no data");
    tSMP_INT_DATA smp_int_data;
    smp_int_data.status = SMP_OOB_FAIL;
    smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
    return;
  }

  LOG_VERBOSE(
      "req_oob_type:%d, loc_oob_data.present:%d, "
      "peer_oob_data.present:%d",
  log::verbose(
      "req_oob_type:{}, loc_oob_data.present:{}, peer_oob_data.present:{}",
      p_cb->req_oob_type, p_oob->loc_oob_data.present,
      p_oob->peer_oob_data.present);

@@ -441,7 +443,7 @@ void SMP_SecureConnectionOobDataReply(uint8_t* p_data) {
        data_missing = true;
      break;
    default:
      LOG_VERBOSE("Unexpected OOB data type requested. Fail OOB");
      log::verbose("Unexpected OOB data type requested. Fail OOB");
      data_missing = true;
      break;
  }
@@ -499,16 +501,16 @@ void SMP_ClearLocScOobData() { smp_clear_local_oob_data(); }
void SMP_SirkConfirmDeviceReply(const RawAddress& bd_addr, uint8_t res) {
  tSMP_CB* p_cb = &smp_cb;

  LOG_INFO("Result:%d", res);
  log::info("Result:{}", res);

  /* If timeout already expired or has been canceled, ignore the reply */
  if (p_cb->cb_evt != SMP_SIRK_VERIFICATION_REQ_EVT) {
    LOG_WARN("Wrong State:%d", p_cb->state);
    log::warn("Wrong State:{}", p_cb->state);
    return;
  }

  if (bd_addr != p_cb->pairing_bda) {
    LOG_WARN("Wrong confirmation BD Addr: %s vs expected %s",
    log::warn("Wrong confirmation BD Addr: {} vs expected {}",
              ADDRESS_TO_LOGGABLE_CSTR(bd_addr),
              ADDRESS_TO_LOGGABLE_CSTR(p_cb->pairing_bda));
    return;
@@ -516,7 +518,7 @@ void SMP_SirkConfirmDeviceReply(const RawAddress& bd_addr, uint8_t res) {

  tSMP_INT_DATA smp_int_data;
  if (res != SMP_SUCCESS) {
    LOG_WARN("Verification fails");
    log::warn("Verification fails");
    /* send pairing failure */
    smp_int_data.status = SMP_SIRK_DEVICE_INVALID;
    smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
+18 −15
Original line number Diff line number Diff line
@@ -18,10 +18,14 @@

#define LOG_TAG "smp"

#include <bluetooth/log.h>

#include "os/log.h"
#include "smp_int.h"
#include "types/hci_role.h"

using namespace bluetooth;

const char* const smp_br_state_name[SMP_BR_STATE_MAX + 1] = {
    "SMP_BR_STATE_IDLE", "SMP_BR_STATE_WAIT_APP_RSP",
    "SMP_BR_STATE_PAIR_REQ_RSP", "SMP_BR_STATE_BOND_PENDING",
@@ -246,12 +250,12 @@ static const tSMP_BR_ENTRY_TBL smp_br_entry_table[] = {
 ******************************************************************************/
void smp_set_br_state(tSMP_BR_STATE br_state) {
  if (br_state < SMP_BR_STATE_MAX) {
    LOG_VERBOSE("BR_State change:%s(%d)==>%s(%d)",
    log::verbose("BR_State change:{}({})==>{}({})",
                 smp_get_br_state_name(smp_cb.br_state), smp_cb.br_state,
                 smp_get_br_state_name(br_state), br_state);
    smp_cb.br_state = br_state;
  } else {
    LOG_VERBOSE("invalid br_state=%d", br_state);
    log::verbose("invalid br_state={}", br_state);
  }
}

@@ -305,23 +309,22 @@ void smp_br_state_machine_event(tSMP_CB* p_cb, tSMP_BR_EVENT event,
  tSMP_BR_SM_TBL state_table;
  uint8_t action, entry;

  LOG_DEBUG("addr:%s", ADDRESS_TO_LOGGABLE_CSTR(p_cb->pairing_bda));
  log::debug("addr:{}", ADDRESS_TO_LOGGABLE_CSTR(p_cb->pairing_bda));
  if (curr_state >= SMP_BR_STATE_MAX) {
    LOG_ERROR("Invalid br_state: %d", curr_state);
    log::error("Invalid br_state: {}", curr_state);
    return;
  }

  if (p_cb->role > HCI_ROLE_PERIPHERAL) {
    LOG_ERROR("invalid role %d", p_cb->role);
    log::error("invalid role {}", p_cb->role);
    return;
  }

  tSMP_BR_ENTRY_TBL entry_table = smp_br_entry_table[p_cb->role];

  LOG_DEBUG("Role:%s State:[%s(%d)], Event:[%s(%d)]",
            hci_role_text(p_cb->role).c_str(),
            smp_get_br_state_name(p_cb->br_state), p_cb->br_state,
            smp_get_br_event_name(event), event);
  log::debug("Role:{} State:[{}({})], Event:[{}({})]",
             hci_role_text(p_cb->role), smp_get_br_state_name(p_cb->br_state),
             p_cb->br_state, smp_get_br_event_name(event), event);

  /* look up the state table for the current state */
  /* lookup entry / w event & curr_state */
@@ -336,7 +339,7 @@ void smp_br_state_machine_event(tSMP_CB* p_cb, tSMP_BR_EVENT event,
      state_table = smp_br_state_table[curr_state][p_cb->role];
    }
  } else {
    LOG_VERBOSE("Ignore event[%s(%d)] in state[%s(%d)]",
    log::verbose("Ignore event[{}({})] in state[{}({})]",
                 smp_get_br_event_name(event), event,
                 smp_get_br_state_name(curr_state), curr_state);
    return;
@@ -358,5 +361,5 @@ void smp_br_state_machine_event(tSMP_CB* p_cb, tSMP_BR_EVENT event,
      break;
    }
  }
  LOG_VERBOSE("result state=%s", smp_get_br_state_name(p_cb->br_state));
  log::verbose("result state={}", smp_get_br_state_name(p_cb->br_state));
}
Loading