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

Commit 1cb0b5d3 authored by Łukasz Rymanowski's avatar Łukasz Rymanowski Committed by Automerger Merge Worker
Browse files

connection_manager: Integrate targeted annoucement filtering am: 9eb944b8

parents 3e198501 9eb944b8
Loading
Loading
Loading
Loading
+4 −1
Original line number Original line Diff line number Diff line
@@ -486,23 +486,26 @@ cc_test {
    local_include_dirs: [
    local_include_dirs: [
        "include",
        "include",
        "btm",
        "btm",
        "test/common",
    ],
    ],
    include_dirs: [
    include_dirs: [
        "packages/modules/Bluetooth/system",
        "packages/modules/Bluetooth/system",
        "packages/modules/Bluetooth/system/gd",
        "packages/modules/Bluetooth/system/gd",
        "packages/modules/Bluetooth/system/internal_include",
        "packages/modules/Bluetooth/system/internal_include",
        "packages/modules/Bluetooth/system/internal_include",
        "packages/modules/Bluetooth/system/utils/include",
        "packages/modules/Bluetooth/system/utils/include",
    ],
    ],
    srcs: [
    srcs: [
        "gatt/connection_manager.cc",
        "gatt/connection_manager.cc",
        "test/common/mock_btm_api_layer.cc",
        "test/gatt_connection_manager_test.cc",
        "test/gatt_connection_manager_test.cc",
        ":TestCommonMainHandler",
    ],
    ],
    shared_libs: [
    shared_libs: [
        "libcutils",
        "libcutils",
    ],
    ],
    static_libs: [
    static_libs: [
        "libbluetooth-types",
        "libbluetooth-types",
        "libbt-common",
        "liblog",
        "liblog",
        "libgmock",
        "libgmock",
    ],
    ],
+90 −1
Original line number Original line Diff line number Diff line
@@ -27,11 +27,16 @@
#include <memory>
#include <memory>
#include <set>
#include <set>


#include "bind_helpers.h"
#include "internal_include/bt_trace.h"
#include "internal_include/bt_trace.h"
#include "main/shim/le_scanning_manager.h"
#include "main/shim/shim.h"
#include "main/shim/shim.h"
#include "osi/include/alarm.h"
#include "osi/include/alarm.h"
#include "osi/include/log.h"
#include "osi/include/log.h"
#include "stack/btm/btm_ble_bgconn.h"
#include "stack/btm/btm_ble_bgconn.h"
#include "stack/include/advertise_data_parser.h"
#include "stack/include/btm_ble_api.h"
#include "stack/include/btu.h"  // do_in_main_thread
#include "stack/include/l2c_api.h"
#include "stack/include/l2c_api.h"
#include "types/raw_address.h"
#include "types/raw_address.h"


@@ -112,8 +117,86 @@ std::set<tAPP_ID> get_apps_connecting_to(const RawAddress& address) {
                                  : std::set<tAPP_ID>();
                                  : std::set<tAPP_ID>();
}
}


bool IsTargetedAnnouncement(const uint8_t* p_eir, uint16_t eir_len) {
  const uint8_t* p_service_data = p_eir;
  uint16_t remaining_data_len = eir_len;
  uint8_t service_data_len = 0;

  while ((p_service_data = AdvertiseDataParser::GetFieldByType(
              p_service_data + service_data_len,
              (remaining_data_len -= service_data_len),
              BTM_BLE_AD_TYPE_SERVICE_DATA_TYPE, &service_data_len))) {
    uint16_t uuid;
    uint8_t announcement_type;
    const uint8_t* p_tmp = p_service_data;

    if (service_data_len < 1) {
      continue;
    }

    STREAM_TO_UINT16(uuid, p_tmp);
    LOG_DEBUG("Found UUID 0x%04x", uuid);

    if (uuid != 0x184E && uuid != 0x1853) {
      continue;
    }

    STREAM_TO_UINT8(announcement_type, p_tmp);
    LOG_DEBUG("Found announcement_type 0x%02x", announcement_type);
    if (announcement_type == 0x01) {
      return true;
    }
  }
  return false;
}

static void schedule_direct_connect_add(uint8_t app_id,
                                        const RawAddress& address);

static void target_announcement_observe_results_cb(tBTM_INQ_RESULTS* p_inq,
                                                   const uint8_t* p_eir,
                                                   uint16_t eir_len) {
  auto addr = p_inq->remote_bd_addr;
  auto it = bgconn_dev.find(addr);
  if (it == bgconn_dev.end() ||
      it->second.doing_targeted_announcements_conn.empty()) {
    return;
  }

  if (!IsTargetedAnnouncement(p_eir, eir_len)) {
    LOG_DEBUG("Not a targeted announcement for device %s",
              addr.ToString().c_str());
    return;
  }

  LOG_INFO("Found targeted announcement for device %s",
           addr.ToString().c_str());

  if (it->second.is_in_accept_list) {
    LOG_INFO("Device %s is already connecting", addr.ToString().c_str());
    return;
  }

  if (BTM_GetHCIConnHandle(addr, BT_TRANSPORT_LE) != 0xFFFF) {
    LOG_DEBUG("Device %s already connected", addr.ToString().c_str());
    return;
  }

  /* Take fist app_id and use it for direct_connect */
  auto app_id = *(it->second.doing_targeted_announcements_conn.begin());

  /* If scan is ongoing lets stop it */
  do_in_main_thread(FROM_HERE,
                    base::BindOnce(schedule_direct_connect_add, app_id, addr));
}

void target_announcements_filtering_set(bool enable) {
void target_announcements_filtering_set(bool enable) {
  // TODO Implement
  LOG_DEBUG("enable %d", enable);
  /* Safe to call as if there is no support for filtering, this call will be
   * ignored. */
  bluetooth::shim::set_target_announcements_filter(enable);
  BTM_BleTargetAnnouncementObserve(enable,
                                   target_announcement_observe_results_cb);
}
}


/** Add a device to the background connection list for targeted announcements.
/** Add a device to the background connection list for targeted announcements.
@@ -405,9 +488,15 @@ bool direct_connect_add(uint8_t app_id, const RawAddress& address) {


  bgconn_dev[address].doing_direct_conn.emplace(
  bgconn_dev[address].doing_direct_conn.emplace(
      app_id, unique_alarm_ptr(timeout, &alarm_free));
      app_id, unique_alarm_ptr(timeout, &alarm_free));

  return true;
  return true;
}
}


static void schedule_direct_connect_add(uint8_t app_id,
                                        const RawAddress& address) {
  direct_connect_add(app_id, address);
}

static bool any_direct_connect_left() {
static bool any_direct_connect_left() {
  for (const auto& tmp : bgconn_dev) {
  for (const auto& tmp : bgconn_dev) {
    if (!tmp.second.doing_direct_conn.empty()) return true;
    if (!tmp.second.doing_direct_conn.empty()) return true;
+15 −0
Original line number Original line Diff line number Diff line
#include <base/bind.h>
#include <base/bind.h>
#include <base/bind_helpers.h>
#include <base/callback.h>
#include <base/callback.h>
#include <base/location.h>
#include <base/location.h>
#include <gmock/gmock.h>
#include <gmock/gmock.h>
@@ -10,6 +11,7 @@
#include "osi/include/alarm.h"
#include "osi/include/alarm.h"
#include "osi/test/alarm_mock.h"
#include "osi/test/alarm_mock.h"
#include "stack/gatt/connection_manager.h"
#include "stack/gatt/connection_manager.h"
#include "stack/test/common/mock_btm_api_layer.h"


using testing::_;
using testing::_;
using testing::DoAll;
using testing::DoAll;
@@ -35,6 +37,10 @@ class AcceptlistMock {
  MOCK_METHOD0(SetLeConnectionModeToFast, bool());
  MOCK_METHOD0(SetLeConnectionModeToFast, bool());
  MOCK_METHOD0(SetLeConnectionModeToSlow, void());
  MOCK_METHOD0(SetLeConnectionModeToSlow, void());
  MOCK_METHOD2(OnConnectionTimedOut, void(uint8_t, const RawAddress&));
  MOCK_METHOD2(OnConnectionTimedOut, void(uint8_t, const RawAddress&));

  /* Not really accept list related, btui still BTM - just for testing put it
   * here. */
  MOCK_METHOD2(EnableTargetedAnnouncements, void(bool, tBTM_INQ_RESULTS_CB*));
};
};


std::unique_ptr<AcceptlistMock> localAcceptlistMock;
std::unique_ptr<AcceptlistMock> localAcceptlistMock;
@@ -67,15 +73,24 @@ void BTM_SetLeConnectionModeToSlow() {
  localAcceptlistMock->SetLeConnectionModeToSlow();
  localAcceptlistMock->SetLeConnectionModeToSlow();
}
}


void BTM_BleTargetAnnouncementObserve(bool enable,
                                      tBTM_INQ_RESULTS_CB* p_results_cb) {
  localAcceptlistMock->EnableTargetedAnnouncements(enable, p_results_cb);
}

namespace bluetooth {
namespace bluetooth {
namespace shim {
namespace shim {
bool is_gd_l2cap_enabled() { return false; }
bool is_gd_l2cap_enabled() { return false; }
void set_target_announcements_filter(bool enable) {}
}  // namespace shim
}  // namespace shim
}  // namespace bluetooth
}  // namespace bluetooth


bool L2CA_ConnectFixedChnl(uint16_t fixed_cid, const RawAddress& bd_addr) {
bool L2CA_ConnectFixedChnl(uint16_t fixed_cid, const RawAddress& bd_addr) {
  return false;
  return false;
}
}
uint16_t BTM_GetHCIConnHandle(RawAddress const&, unsigned char) {
  return 0xFFFF;
};


namespace connection_manager {
namespace connection_manager {
class BleConnectionManager : public testing::Test {
class BleConnectionManager : public testing::Test {