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

Commit adef9979 authored by Myles Watson's avatar Myles Watson Committed by Gerrit Code Review
Browse files

Merge changes I2aa66c87,Ibfa735b4,Iee4831c2 into main

* changes:
  btm_inq: Use bluetooth::hci:: types
  Move Init and Free to btm_inq.cc
  btm_inq cpplint
parents 2bbdd744 3b7ab606
Loading
Loading
Loading
Loading
+128 −92
Original line number Diff line number Diff line
@@ -35,8 +35,6 @@

#include <mutex>

#include "advertise_data_parser.h"
#include "bt_name.h"
#include "btif/include/btif_acl.h"
#include "btif/include/btif_config.h"
#include "common/time_util.h"
@@ -48,7 +46,6 @@
#include "main/shim/entry.h"
#include "main/shim/helpers.h"
#include "main/shim/shim.h"
#include "neighbor_inquiry.h"
#include "osi/include/allocator.h"
#include "osi/include/properties.h"
#include "osi/include/stack_power_telemetry.h"
@@ -58,8 +55,10 @@
#include "stack/btm/btm_sec.h"
#include "stack/btm/neighbor_inquiry.h"
#include "stack/include/acl_api_types.h"
#include "stack/include/advertise_data_parser.h"
#include "stack/include/bt_hdr.h"
#include "stack/include/bt_lap.h"
#include "stack/include/bt_name.h"
#include "stack/include/bt_types.h"
#include "stack/include/bt_uuid16.h"
#include "stack/include/btm_ble_api.h"
@@ -131,6 +130,20 @@ void btm_ble_stop_inquiry(void);

using namespace bluetooth;
using bluetooth::Uuid;
using bluetooth::hci::CommandCompleteView;
using bluetooth::hci::CommandStatusView;
using bluetooth::hci::ErrorCode;
using bluetooth::hci::EventCode;
using bluetooth::hci::EventView;
using bluetooth::hci::ExtendedInquiryResultView;
using bluetooth::hci::GapDataType;
using bluetooth::hci::InquiryBuilder;
using bluetooth::hci::InquiryCancelBuilder;
using bluetooth::hci::InquiryCancelCompleteView;
using bluetooth::hci::InquiryCompleteView;
using bluetooth::hci::InquiryResultView;
using bluetooth::hci::InquiryResultWithRssiView;
using bluetooth::hci::Lap;

/* 3 second timeout waiting for responses */
#define BTM_INQ_REPLY_TIMEOUT_MS (3 * 1000)
@@ -242,7 +255,7 @@ static const uint8_t* btm_eir_get_uuid_list(const uint8_t* p_eir, size_t eir_len
                                            uint8_t* p_num_uuid, uint8_t* p_uuid_list_type);

static void btm_process_cancel_complete(tHCI_STATUS status, uint8_t mode);
static void on_incoming_hci_event(bluetooth::hci::EventView event);
static void on_incoming_hci_event(EventView event);
static bool is_inquery_by_rssi() { return osi_property_get_bool(PROPERTY_INQ_BY_RSSI, false); }
/*******************************************************************************
 *
@@ -575,10 +588,9 @@ void BTM_CancelInquiry(void) {

    if ((btm_cb.btm_inq_vars.inqparms.mode & BTM_GENERAL_INQUIRY) != 0) {
      bluetooth::shim::GetHciLayer()->EnqueueCommand(
              bluetooth::hci::InquiryCancelBuilder::Create(),
              get_main_thread()->BindOnce([](bluetooth::hci::CommandCompleteView complete_view) {
                bluetooth::hci::check_complete<bluetooth::hci::InquiryCancelCompleteView>(
                        complete_view);
              InquiryCancelBuilder::Create(),
              get_main_thread()->BindOnce([](CommandCompleteView complete_view) {
                check_complete<InquiryCancelCompleteView>(complete_view);
                btm_process_cancel_complete(HCI_SUCCESS, BTM_GENERAL_INQUIRY);
              }));
    }
@@ -679,21 +691,17 @@ tBTM_STATUS BTM_StartInquiry(tBTM_INQ_RESULTS_CB* p_results_cb, tBTM_CMPL_CB* p_

  if (btm_cb.btm_inq_vars.registered_for_hci_events == false) {
    bluetooth::shim::GetHciLayer()->RegisterEventHandler(
            bluetooth::hci::EventCode::INQUIRY_COMPLETE,
            get_main_thread()->Bind(
                    [](bluetooth::hci::EventView event) { on_incoming_hci_event(event); }));
            EventCode::INQUIRY_COMPLETE,
            get_main_thread()->Bind([](EventView event) { on_incoming_hci_event(event); }));
    bluetooth::shim::GetHciLayer()->RegisterEventHandler(
            bluetooth::hci::EventCode::INQUIRY_RESULT,
            get_main_thread()->Bind(
                    [](bluetooth::hci::EventView event) { on_incoming_hci_event(event); }));
            EventCode::INQUIRY_RESULT,
            get_main_thread()->Bind([](EventView event) { on_incoming_hci_event(event); }));
    bluetooth::shim::GetHciLayer()->RegisterEventHandler(
            bluetooth::hci::EventCode::INQUIRY_RESULT_WITH_RSSI,
            get_main_thread()->Bind(
                    [](bluetooth::hci::EventView event) { on_incoming_hci_event(event); }));
            EventCode::INQUIRY_RESULT_WITH_RSSI,
            get_main_thread()->Bind([](EventView event) { on_incoming_hci_event(event); }));
    bluetooth::shim::GetHciLayer()->RegisterEventHandler(
            bluetooth::hci::EventCode::EXTENDED_INQUIRY_RESULT,
            get_main_thread()->Bind(
                    [](bluetooth::hci::EventView event) { on_incoming_hci_event(event); }));
            EventCode::EXTENDED_INQUIRY_RESULT,
            get_main_thread()->Bind([](EventView event) { on_incoming_hci_event(event); }));

    btm_cb.btm_inq_vars.registered_for_hci_events = true;
  }
@@ -745,20 +753,19 @@ tBTM_STATUS BTM_StartInquiry(tBTM_INQ_RESULTS_CB* p_results_cb, tBTM_CMPL_CB* p_

  btm_init_inq_result_flt();

  bluetooth::hci::Lap lap;
  Lap lap;
  lap.lap_ = general_inq_lap[2];

  // TODO: Register for the inquiry interface and use that
  bluetooth::shim::GetHciLayer()->EnqueueCommand(
          bluetooth::hci::InquiryBuilder::Create(lap, btm_cb.btm_inq_vars.inqparms.duration, 0),
          get_main_thread()->BindOnce([](bluetooth::hci::CommandStatusView status_view) {
          InquiryBuilder::Create(lap, btm_cb.btm_inq_vars.inqparms.duration, 0),
          get_main_thread()->BindOnce([](CommandStatusView status_view) {
            log::assert_that(status_view.IsValid(), "assert failed: status_view.IsValid()");
            auto status = status_view.GetStatus();
            if (status == bluetooth::hci::ErrorCode::SUCCESS) {
            if (status == ErrorCode::SUCCESS) {
              BTIF_dm_report_inquiry_status_change(tBTM_INQUIRY_STATE::BTM_INQUIRY_STARTED);
            } else {
              log::info("Inquiry failed to start status: {}",
                        bluetooth::hci::ErrorCodeText(status));
              log::info("Inquiry failed to start status: {}", ErrorCodeText(status));
            }
          }));

@@ -899,7 +906,7 @@ tBTM_INQ_INFO* BTM_InqDbFirst(void) {
  }

  /* If here, no used entry found */
  return (tBTM_INQ_INFO*)NULL;
  return nullptr;
}

/*******************************************************************************
@@ -929,7 +936,7 @@ tBTM_INQ_INFO* BTM_InqDbNext(tBTM_INQ_INFO* p_cur) {
    }

    /* If here, more entries found */
    return (tBTM_INQ_INFO*)NULL;
    return nullptr;
  } else {
    return BTM_InqDbFirst();
  }
@@ -1011,8 +1018,8 @@ void btm_inq_db_reset(void) {

  /* If an inquiry or periodic inquiry is active, reset the mode to inactive */
  if (btm_cb.btm_inq_vars.inq_active != BTM_INQUIRY_INACTIVE) {
    temp_inq_active = btm_cb.btm_inq_vars.inq_active; /* Save so state can change BEFORE
                                                callback is called */
    /* Save so state can change BEFORE callback is called */
    temp_inq_active = btm_cb.btm_inq_vars.inq_active;
    btm_cb.btm_inq_vars.inq_active = BTM_INQUIRY_INACTIVE;

    /* If not a periodic inquiry, the complete callback must be called to notify
@@ -1184,7 +1191,7 @@ tINQ_DB_ENT* btm_inq_db_find(const RawAddress& p_bda) {
  }

  /* If here, not found */
  return NULL;
  return nullptr;
}

/*******************************************************************************
@@ -1255,7 +1262,7 @@ tINQ_DB_ENT* btm_inq_db_new(const RawAddress& p_bda, bool is_ble) {
 * Returns          void
 *
 ******************************************************************************/
static void btm_process_inq_results_standard(bluetooth::hci::EventView event) {
static void btm_process_inq_results_standard(EventView event) {
  RawAddress bda;
  tINQ_DB_ENT* p_i;
  tBTM_INQ_RESULTS* p_cur = NULL;
@@ -1277,7 +1284,7 @@ static void btm_process_inq_results_standard(bluetooth::hci::EventView event) {
    return;
  }

  auto standard_view = bluetooth::hci::InquiryResultView::Create(event);
  auto standard_view = InquiryResultView::Create(event);
  log::assert_that(standard_view.IsValid(), "assert failed: standard_view.IsValid()");
  auto responses = standard_view.GetResponses();

@@ -1302,16 +1309,16 @@ static void btm_process_inq_results_standard(bluetooth::hci::EventView event) {
    if (p_i == NULL) {
      p_i = btm_inq_db_new(bda, false);
      is_new = true;
    }

    } else {
      /* If an entry for the device already exists, overwrite it ONLY if it is
         from a previous inquiry. (Ignore it if it is a duplicate response from
         the same inquiry.
      */
    else if (p_i->inq_count == btm_cb.btm_inq_vars.inq_counter &&
      if (p_i->inq_count == btm_cb.btm_inq_vars.inq_counter &&
          (p_i->inq_info.results.device_type == BT_DEVICE_TYPE_BREDR)) {
        is_new = false;
      }
    }

    p_i->inq_info.results.rssi = BTM_INQ_RES_IGNORE_RSSI;

@@ -1371,7 +1378,7 @@ static void btm_process_inq_results_standard(bluetooth::hci::EventView event) {
 * Returns          void
 *
 ******************************************************************************/
static void btm_process_inq_results_rssi(bluetooth::hci::EventView event) {
static void btm_process_inq_results_rssi(EventView event) {
  RawAddress bda;
  tINQ_DB_ENT* p_i;
  tBTM_INQ_RESULTS* p_cur = NULL;
@@ -1396,7 +1403,7 @@ static void btm_process_inq_results_rssi(bluetooth::hci::EventView event) {
    return;
  }

  auto rssi_view = bluetooth::hci::InquiryResultWithRssiView::Create(event);
  auto rssi_view = InquiryResultWithRssiView::Create(event);
  log::assert_that(rssi_view.IsValid(), "assert failed: rssi_view.IsValid()");
  auto responses = rssi_view.GetResponses();

@@ -1433,9 +1440,8 @@ static void btm_process_inq_results_rssi(bluetooth::hci::EventView event) {
        log::verbose("update RSSI new:{}, old:{}", i_rssi, p_cur->rssi);
        p_cur->rssi = i_rssi;
        update = true;
      }
      } else {
        /* If no update needed continue with next response (if any) */
      else {
        continue;
      }
    }
@@ -1445,16 +1451,16 @@ static void btm_process_inq_results_rssi(bluetooth::hci::EventView event) {
    if (p_i == NULL) {
      p_i = btm_inq_db_new(bda, false);
      is_new = true;
    }

    } else {
      /* If an entry for the device already exists, overwrite it ONLY if it is
         from a previous inquiry. (Ignore it if it is a duplicate response from
         the same inquiry.
      */
    else if (p_i->inq_count == btm_cb.btm_inq_vars.inq_counter &&
      if (p_i->inq_count == btm_cb.btm_inq_vars.inq_counter &&
          (p_i->inq_info.results.device_type == BT_DEVICE_TYPE_BREDR)) {
        is_new = false;
      }
    }

    /* keep updating RSSI to have latest value */
    p_i->inq_info.results.rssi = (int8_t)rssi;
@@ -1515,7 +1521,7 @@ static void btm_process_inq_results_rssi(bluetooth::hci::EventView event) {
 * Returns          void
 *
 ******************************************************************************/
static void btm_process_inq_results_extended(bluetooth::hci::EventView event) {
static void btm_process_inq_results_extended(EventView event) {
  RawAddress bda;
  tINQ_DB_ENT* p_i;
  tBTM_INQ_RESULTS* p_cur = NULL;
@@ -1539,7 +1545,7 @@ static void btm_process_inq_results_extended(bluetooth::hci::EventView event) {
    return;
  }

  auto extended_view = bluetooth::hci::ExtendedInquiryResultView::Create(event);
  auto extended_view = ExtendedInquiryResultView::Create(event);
  log::assert_that(extended_view.IsValid(), "assert failed: extended_view.IsValid()");

  btm_cb.neighbor.classic_inquiry.results++;
@@ -1573,37 +1579,37 @@ static void btm_process_inq_results_extended(bluetooth::hci::EventView event) {
        log::verbose("update RSSI new:{}, old:{}", i_rssi, p_cur->rssi);
        p_cur->rssi = i_rssi;
        update = true;
      }
      } else {
        /* If we received a second Extended Inq Event for an already */
        /* discovered device, this is because for the first one EIR was not
           received */
      else if (p_i) {
        if (p_i) {
          p_cur = &p_i->inq_info.results;
          update = true;
      }
        } else {
          /* If no update needed continue with next response (if any) */
      else {
          return;
        }
      }
    }

    /* If existing entry, use that, else get a new one (possibly reusing the
     * oldest) */
    if (p_i == NULL) {
      p_i = btm_inq_db_new(bda, false);
      is_new = true;
    }

    } else {
      /* If an entry for the device already exists, overwrite it ONLY if it is
         from
         a previous inquiry. (Ignore it if it is a duplicate response from the
         same
         inquiry.
      */
    else if (p_i->inq_count == btm_cb.btm_inq_vars.inq_counter &&
      if (p_i->inq_count == btm_cb.btm_inq_vars.inq_counter &&
          (p_i->inq_info.results.device_type == BT_DEVICE_TYPE_BREDR)) {
        is_new = false;
      }
    }

    /* keep updating RSSI to have latest value */
    p_i->inq_info.results.rssi = (int8_t)rssi;
@@ -1646,7 +1652,7 @@ static void btm_process_inq_results_extended(bluetooth::hci::EventView event) {
      data.reserve(HCI_EXT_INQ_RESPONSE_LEN);
      bluetooth::packet::BitInserter bi(data);
      for (const auto& eir : extended_view.GetExtendedInquiryResponse()) {
        if (eir.data_type_ != static_cast<bluetooth::hci::GapDataType>(0)) {
        if (eir.data_type_ != static_cast<GapDataType>(0)) {
          eir.Serialize(bi);
        }
      }
@@ -2135,9 +2141,8 @@ uint8_t BTM_GetEirSupportedServices(uint32_t* p_eir_uuid, uint8_t** p, uint8_t m
      if (*p_num_uuid16 < max_num_uuid16) {
        UINT16_TO_STREAM(*p, BTM_EIR_UUID_LKUP_TBL[service_index]);
        (*p_num_uuid16)++;
      }
      } else {
        /* if max number of UUIDs are stored and found one more */
      else {
        return HCI_EIR_MORE_16BITS_UUID_TYPE;
      }
    }
@@ -2385,8 +2390,8 @@ void btm_set_eir_uuid(const uint8_t* p_eir, tBTM_INQ_RESULTS* p_results) {
  }
}

static void on_inquiry_complete(bluetooth::hci::EventView event) {
  auto complete = bluetooth::hci::InquiryCompleteView::Create(event);
static void on_inquiry_complete(EventView event) {
  auto complete = InquiryCompleteView::Create(event);
  log::assert_that(complete.IsValid(), "assert failed: complete.IsValid()");
  auto status = to_hci_status_code(static_cast<uint8_t>(complete.GetStatus()));

@@ -2403,27 +2408,58 @@ static void on_inquiry_complete(bluetooth::hci::EventView event) {
 * Returns          None
 *
 ******************************************************************************/
static void on_incoming_hci_event(bluetooth::hci::EventView event) {
static void on_incoming_hci_event(EventView event) {
  log::assert_that(event.IsValid(), "assert failed: event.IsValid()");
  auto event_code = event.GetEventCode();
  switch (event_code) {
    case bluetooth::hci::EventCode::INQUIRY_COMPLETE:
    case EventCode::INQUIRY_COMPLETE:
      on_inquiry_complete(event);
      break;
    case bluetooth::hci::EventCode::INQUIRY_RESULT:
    case EventCode::INQUIRY_RESULT:
      btm_process_inq_results_standard(event);
      break;
    case bluetooth::hci::EventCode::INQUIRY_RESULT_WITH_RSSI:
    case EventCode::INQUIRY_RESULT_WITH_RSSI:
      btm_process_inq_results_rssi(event);
      break;
    case bluetooth::hci::EventCode::EXTENDED_INQUIRY_RESULT:
    case EventCode::EXTENDED_INQUIRY_RESULT:
      btm_process_inq_results_extended(event);
      break;
    default:
      log::warn("Dropping unhandled event: {}", bluetooth::hci::EventCodeText(event_code));
      log::warn("Dropping unhandled event: {}", EventCodeText(event_code));
  }
}

void tBTM_INQUIRY_VAR_ST::Init() {
  alarm_free(classic_inquiry_timer);

  classic_inquiry_timer = alarm_new("btm_inq.classic_inquiry_timer");

  discoverable_mode = BTM_NON_DISCOVERABLE;
  connectable_mode = BTM_NON_CONNECTABLE;

  page_scan_window = HCI_DEF_PAGESCAN_WINDOW;
  page_scan_period = HCI_DEF_PAGESCAN_INTERVAL;
  inq_scan_window = HCI_DEF_INQUIRYSCAN_WINDOW;
  inq_scan_period = HCI_DEF_INQUIRYSCAN_INTERVAL;
  inq_scan_type = BTM_SCAN_TYPE_STANDARD;
  page_scan_type = HCI_DEF_SCAN_TYPE;

  p_inq_cmpl_cb = nullptr;
  p_inq_results_cb = nullptr;

  inq_counter = 0;
  inqparms = {};
  inq_cmpl_info = {};

  per_min_delay = 0;
  per_max_delay = 0;
  state = BTM_INQ_INACTIVE_STATE;
  inq_active = 0;
  registered_for_hci_events = false;
}

void tBTM_INQUIRY_VAR_ST::Free() { alarm_free(classic_inquiry_timer); }

namespace bluetooth {
namespace legacy {
namespace testing {
+2 −29
Original line number Diff line number Diff line
@@ -228,35 +228,8 @@ struct tBTM_INQUIRY_VAR_ST {

  bool registered_for_hci_events;

  void Init() {
    alarm_free(classic_inquiry_timer);

    classic_inquiry_timer = alarm_new("btm_inq.classic_inquiry_timer");

    discoverable_mode = BTM_NON_DISCOVERABLE;
    connectable_mode = BTM_NON_CONNECTABLE;

    page_scan_window = HCI_DEF_PAGESCAN_WINDOW;
    page_scan_period = HCI_DEF_PAGESCAN_INTERVAL;
    inq_scan_window = HCI_DEF_INQUIRYSCAN_WINDOW;
    inq_scan_period = HCI_DEF_INQUIRYSCAN_INTERVAL;
    inq_scan_type = BTM_SCAN_TYPE_STANDARD;
    page_scan_type = HCI_DEF_SCAN_TYPE;

    p_inq_cmpl_cb = nullptr;
    p_inq_results_cb = nullptr;

    inq_counter = 0;
    inqparms = {};
    inq_cmpl_info = {};

    per_min_delay = 0;
    per_max_delay = 0;
    state = BTM_INQ_INACTIVE_STATE;
    inq_active = 0;
    registered_for_hci_events = false;
  }
  void Free() { alarm_free(classic_inquiry_timer); }
  void Init();
  void Free();
};

bool btm_inq_find_bdaddr(const RawAddress& p_bda);