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

Commit 62ec3d0e authored by Chris Manton's avatar Chris Manton Committed by Automerger Merge Worker
Browse files

[19/25] stack::hid Use stack::l2cap::get_interface() am: c0b22769 am: bb4f3ef8

parents 97fbefc5 bb4f3ef8
Loading
Loading
Loading
Loading
+21 −16
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@
#include "stack/hid/hidd_int.h"
#include "stack/include/bt_hdr.h"
#include "stack/include/bt_psm_types.h"
#include "stack/include/l2cap_interface.h"
#include "stack/include/l2cdefs.h"
#include "stack/include/stack_metrics_logging.h"
#include "types/raw_address.h"
@@ -87,7 +88,8 @@ static void hidd_check_config_done() {

    // send outstanding data on intr
    if (hd_cb.pending_data) {
      if (L2CA_DataWrite(p_hcon->intr_cid, hd_cb.pending_data) != tL2CAP_DW_RESULT::SUCCESS) {
      if (stack::l2cap::get_interface().L2CA_DataWrite(p_hcon->intr_cid, hd_cb.pending_data) !=
          tL2CAP_DW_RESULT::SUCCESS) {
        log::warn("Unable to write L2CAP data cid:{} len:{}", p_hcon->intr_cid,
                  hd_cb.pending_data->len);
      }
@@ -116,7 +118,7 @@ static void hidd_l2cif_connect_ind(const RawAddress& bd_addr, uint16_t cid, uint

  if (!hd_cb.allow_incoming) {
    log::warn("incoming connections not allowed, rejecting");
    if (!L2CA_DisconnectReq(cid)) {
    if (!stack::l2cap::get_interface().L2CA_DisconnectReq(cid)) {
      log::warn("Unable to disconnect L2CAP peer:{} cid:{}", p_dev->addr, cid);
    }

@@ -154,7 +156,7 @@ static void hidd_l2cif_connect_ind(const RawAddress& bd_addr, uint16_t cid, uint
  }

  if (!accept) {
    if (!L2CA_DisconnectReq(cid)) {
    if (!stack::l2cap::get_interface().L2CA_DisconnectReq(cid)) {
      log::warn("Unable to disconnect L2CAP cid:{}", cid);
    }
    return;
@@ -283,7 +285,7 @@ static void hidd_l2cif_config_cfm(uint16_t cid, uint16_t /* initiator */, tL2CAP
  if (cid == p_hcon->ctrl_cid) {
    if (p_hcon->conn_flags & HID_CONN_FLAGS_IS_ORIG) {
      p_hcon->disc_reason = HID_L2CAP_CONN_FAIL;
      if ((p_hcon->intr_cid = L2CA_ConnectReqWithSecurity(
      if ((p_hcon->intr_cid = stack::l2cap::get_interface().L2CA_ConnectReqWithSecurity(
                   HID_PSM_INTERRUPT, hd_cb.device.addr, BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT)) ==
          0) {
        hidd_conn_disconnect();
@@ -348,7 +350,7 @@ static void hidd_l2cif_disconnect_ind(uint16_t cid, bool ack_needed) {
}

static void hidd_l2cif_disconnect(uint16_t cid) {
  if (!L2CA_DisconnectReq(cid)) {
  if (!stack::l2cap::get_interface().L2CA_DisconnectReq(cid)) {
    log::warn("Unable to disconnect L2CAP cid:{}", cid);
  }

@@ -368,7 +370,7 @@ static void hidd_l2cif_disconnect(uint16_t cid) {
    p_hcon->intr_cid = 0;

    // now disconnect CTRL
    if (!L2CA_DisconnectReq(p_hcon->ctrl_cid)) {
    if (!stack::l2cap::get_interface().L2CA_DisconnectReq(p_hcon->ctrl_cid)) {
      log::warn("Unable to disconnect L2CAP cid:{}", p_hcon->ctrl_cid);
    }
    p_hcon->ctrl_cid = 0;
@@ -564,17 +566,19 @@ tHID_STATUS hidd_conn_reg(void) {
  hd_cb.l2cap_intr_cfg.mtu_present = TRUE;
  hd_cb.l2cap_intr_cfg.mtu = HID_DEV_MTU_SIZE;

  if (!L2CA_RegisterWithSecurity(HID_PSM_CONTROL, dev_reg_info, false /* enable_snoop */, nullptr,
                                 HID_DEV_MTU_SIZE, 0, BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT)) {
  if (!stack::l2cap::get_interface().L2CA_RegisterWithSecurity(
              HID_PSM_CONTROL, dev_reg_info, false /* enable_snoop */, nullptr, HID_DEV_MTU_SIZE, 0,
              BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT)) {
    log::error("HID Control (device) registration failed");
    log_counter_metrics(android::bluetooth::CodePathCounterKeyEnum::HIDD_ERR_L2CAP_FAILED_CONTROL,
                        1);
    return HID_ERR_L2CAP_FAILED;
  }

  if (!L2CA_RegisterWithSecurity(HID_PSM_INTERRUPT, dev_reg_info, false /* enable_snoop */, nullptr,
                                 HID_DEV_MTU_SIZE, 0, BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT)) {
    L2CA_Deregister(HID_PSM_CONTROL);
  if (!stack::l2cap::get_interface().L2CA_RegisterWithSecurity(
              HID_PSM_INTERRUPT, dev_reg_info, false /* enable_snoop */, nullptr, HID_DEV_MTU_SIZE,
              0, BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT)) {
    stack::l2cap::get_interface().L2CA_Deregister(HID_PSM_CONTROL);
    log::error("HID Interrupt (device) registration failed");
    log_counter_metrics(android::bluetooth::CodePathCounterKeyEnum::HIDD_ERR_L2CAP_FAILED_INTERRUPT,
                        1);
@@ -596,8 +600,8 @@ tHID_STATUS hidd_conn_reg(void) {
void hidd_conn_dereg(void) {
  log::verbose("");

  L2CA_Deregister(HID_PSM_CONTROL);
  L2CA_Deregister(HID_PSM_INTERRUPT);
  stack::l2cap::get_interface().L2CA_Deregister(HID_PSM_CONTROL);
  stack::l2cap::get_interface().L2CA_Deregister(HID_PSM_INTERRUPT);
}

/*******************************************************************************
@@ -634,7 +638,7 @@ tHID_STATUS hidd_conn_initiate(void) {
  p_dev->conn.conn_flags = HID_CONN_FLAGS_IS_ORIG;

  /* Check if L2CAP started the connection process */
  if ((p_dev->conn.ctrl_cid = L2CA_ConnectReqWithSecurity(
  if ((p_dev->conn.ctrl_cid = stack::l2cap::get_interface().L2CA_ConnectReqWithSecurity(
               HID_PSM_CONTROL, p_dev->addr, BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT)) == 0) {
    log::warn("could not start L2CAP connection");
    hd_cb.callback(hd_cb.device.addr, HID_DHOST_EVT_CLOSE, HID_ERR_L2CAP_FAILED, NULL);
@@ -672,7 +676,8 @@ tHID_STATUS hidd_conn_disconnect(void) {

    /* Set l2cap idle timeout to 0 (so ACL link is disconnected
     * immediately after last channel is closed) */
    if (!L2CA_SetIdleTimeoutByBdAddr(hd_cb.device.addr, 0, BT_TRANSPORT_BR_EDR)) {
    if (!stack::l2cap::get_interface().L2CA_SetIdleTimeoutByBdAddr(hd_cb.device.addr, 0,
                                                                   BT_TRANSPORT_BR_EDR)) {
      log::warn("Unable to set L2CAP idle timeout peer:{} transport:{}", hd_cb.device.addr,
                BT_TRANSPORT_BR_EDR);
    }
@@ -787,7 +792,7 @@ tHID_STATUS hidd_conn_send_data(uint8_t channel, uint8_t msg_type, uint8_t param

  log::verbose("report sent");

  if (L2CA_DataWrite(cid, p_buf) == tL2CAP_DW_RESULT::FAILED) {
  if (stack::l2cap::get_interface().L2CA_DataWrite(cid, p_buf) == tL2CAP_DW_RESULT::FAILED) {
    log_counter_metrics(
            android::bluetooth::CodePathCounterKeyEnum::HIDD_ERR_CONGESTED_AT_DATA_WRITE, 1);
    return HID_ERR_CONGESTED;
+1 −1
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@
#include "stack/hid/hid_conn.h"
#include "stack/include/bt_hdr.h"
#include "stack/include/hidd_api.h"
#include "stack/include/l2c_api.h"  // tL2CAP_CFG_INFO && FLOW_SPEC
#include "stack/include/l2cap_types.h"
#include "types/raw_address.h"

enum { HIDD_DEV_NO_CONN, HIDD_DEV_CONNECTED };
+22 −17
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@
#include "stack/include/bt_psm_types.h"
#include "stack/include/btm_client_interface.h"
#include "stack/include/btm_log_history.h"
#include "stack/include/l2cap_interface.h"
#include "stack/include/stack_metrics_logging.h"
#include "types/raw_address.h"

@@ -105,17 +106,19 @@ tHID_STATUS hidh_conn_reg(void) {
  hh_cb.l2cap_cfg.mtu = HID_HOST_MTU;

  /* Now, register with L2CAP */
  if (!L2CA_RegisterWithSecurity(HID_PSM_CONTROL, hst_reg_info, false /* enable_snoop */, nullptr,
                                 HID_HOST_MTU, 0, BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT)) {
  if (!stack::l2cap::get_interface().L2CA_RegisterWithSecurity(
              HID_PSM_CONTROL, hst_reg_info, false /* enable_snoop */, nullptr, HID_HOST_MTU, 0,
              BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT)) {
    log::error("HID-Host Control Registration failed");
    log_counter_metrics(
            android::bluetooth::CodePathCounterKeyEnum::HIDH_ERR_L2CAP_FAILED_AT_REGISTER_CONTROL,
            1);
    return HID_ERR_L2CAP_FAILED;
  }
  if (!L2CA_RegisterWithSecurity(HID_PSM_INTERRUPT, hst_reg_info, false /* enable_snoop */, nullptr,
                                 HID_HOST_MTU, 0, BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT)) {
    L2CA_Deregister(HID_PSM_CONTROL);
  if (!stack::l2cap::get_interface().L2CA_RegisterWithSecurity(
              HID_PSM_INTERRUPT, hst_reg_info, false /* enable_snoop */, nullptr, HID_HOST_MTU, 0,
              BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT)) {
    stack::l2cap::get_interface().L2CA_Deregister(HID_PSM_CONTROL);
    log::error("HID-Host Interrupt Registration failed");
    log_counter_metrics(
            android::bluetooth::CodePathCounterKeyEnum::HIDH_ERR_L2CAP_FAILED_AT_REGISTER_INTERRUPT,
@@ -148,7 +151,8 @@ tHID_STATUS hidh_conn_disconnect(uint8_t dhandle) {

    /* Set l2cap idle timeout to 0 (so ACL link is disconnected
     * immediately after last channel is closed) */
    if (!L2CA_SetIdleTimeoutByBdAddr(hh_cb.devices[dhandle].addr, 0, BT_TRANSPORT_BR_EDR)) {
    if (!stack::l2cap::get_interface().L2CA_SetIdleTimeoutByBdAddr(hh_cb.devices[dhandle].addr, 0,
                                                                   BT_TRANSPORT_BR_EDR)) {
      log::warn("Unable to set L2CAP idle timeout peer:{}", hh_cb.devices[dhandle].addr);
    }
    /* Disconnect both interrupt and control channels */
@@ -185,7 +189,7 @@ static void hidh_l2cif_connect_ind(const RawAddress& bd_addr, uint16_t l2cap_cid

  /* always add incoming connection device into HID database by default */
  if (HID_HostAddDev(bd_addr, HID_SEC_REQUIRED, &i) != HID_SUCCESS) {
    if (!L2CA_DisconnectReq(l2cap_cid)) {
    if (!stack::l2cap::get_interface().L2CA_DisconnectReq(l2cap_cid)) {
      log::warn("Unable to send L2CAP disconnect request peer:{} cid:{}", bd_addr, l2cap_cid);
    }
    return;
@@ -222,7 +226,7 @@ static void hidh_l2cif_connect_ind(const RawAddress& bd_addr, uint16_t l2cap_cid
  }

  if (!bAccept) {
    if (!L2CA_DisconnectReq(l2cap_cid)) {
    if (!stack::l2cap::get_interface().L2CA_DisconnectReq(l2cap_cid)) {
      log::warn("Unable to send L2CAP disconnect request peer:{} cid:{}", bd_addr, l2cap_cid);
    }
    return;
@@ -430,7 +434,8 @@ static void hidh_l2cif_config_cfm(uint16_t l2cap_cid, uint16_t /* initiator */,
                                                    CLOSE_EVT: Connection
                                                    Attempt was made but failed
                                                    */
      p_hcon->intr_cid = L2CA_ConnectReqWithSecurity(HID_PSM_INTERRUPT, hh_cb.devices[dhandle].addr,
      p_hcon->intr_cid = stack::l2cap::get_interface().L2CA_ConnectReqWithSecurity(
              HID_PSM_INTERRUPT, hh_cb.devices[dhandle].addr,
              BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT);
      if (p_hcon->intr_cid == 0) {
        log::warn("HID-Host INTR Originate failed");
@@ -547,7 +552,7 @@ static void hidh_l2cif_disconnect_ind(uint16_t l2cap_cid, bool ack_needed) {
}

static void hidh_l2cif_disconnect(uint16_t l2cap_cid) {
  if (!L2CA_DisconnectReq(l2cap_cid)) {
  if (!stack::l2cap::get_interface().L2CA_DisconnectReq(l2cap_cid)) {
    log::warn("Unable to send L2CAP disconnect request cid:{}", l2cap_cid);
  }

@@ -565,7 +570,7 @@ static void hidh_l2cif_disconnect(uint16_t l2cap_cid) {
    p_hcon->intr_cid = 0;
    if (p_hcon->ctrl_cid) {
      log::verbose("HID-Host Initiating L2CAP Ctrl disconnection");
      if (!L2CA_DisconnectReq(p_hcon->ctrl_cid)) {
      if (!stack::l2cap::get_interface().L2CA_DisconnectReq(p_hcon->ctrl_cid)) {
        log::warn("Unable to send L2CAP disconnect request cid:{}", p_hcon->ctrl_cid);
      }
      p_hcon->ctrl_cid = 0;
@@ -817,7 +822,7 @@ tHID_STATUS hidh_conn_snd_data(uint8_t dhandle, uint8_t trans_type, uint8_t para

    /* Send the buffer through L2CAP */
    if ((p_hcon->conn_flags & HID_CONN_FLAGS_CONGESTED) ||
        (L2CA_DataWrite(cid, p_buf) == tL2CAP_DW_RESULT::FAILED)) {
        (stack::l2cap::get_interface().L2CA_DataWrite(cid, p_buf) == tL2CAP_DW_RESULT::FAILED)) {
      log_counter_metrics(
              android::bluetooth::CodePathCounterKeyEnum::HIDH_ERR_CONGESTED_AT_SEND_DATA, 1);
      return HID_ERR_CONGESTED;
@@ -861,8 +866,8 @@ tHID_STATUS hidh_conn_initiate(uint8_t dhandle) {
  p_dev->conn.conn_flags = HID_CONN_FLAGS_IS_ORIG;

  /* Check if L2CAP started the connection process */
  p_dev->conn.ctrl_cid = L2CA_ConnectReqWithSecurity(HID_PSM_CONTROL, p_dev->addr,
                                                     BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT);
  p_dev->conn.ctrl_cid = stack::l2cap::get_interface().L2CA_ConnectReqWithSecurity(
          HID_PSM_CONTROL, p_dev->addr, BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT);
  if (p_dev->conn.ctrl_cid == 0) {
    log::warn("HID-Host Originate failed");
    hh_cb.callback(dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_CLOSE, HID_ERR_L2CAP_FAILED,
@@ -904,8 +909,8 @@ static uint8_t find_conn_by_cid(uint16_t cid) {
}

void hidh_conn_dereg(void) {
  L2CA_Deregister(HID_PSM_CONTROL);
  L2CA_Deregister(HID_PSM_INTERRUPT);
  stack::l2cap::get_interface().L2CA_Deregister(HID_PSM_CONTROL);
  stack::l2cap::get_interface().L2CA_Deregister(HID_PSM_INTERRUPT);
}

/*******************************************************************************
+1 −1
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@
#include "stack/hid/hid_conn.h"
#include "stack/include/bt_hdr.h"
#include "stack/include/hidh_api.h"
#include "stack/include/l2c_api.h"  // tL2CAP_CFG_INFO
#include "stack/include/l2cap_types.h"
#include "types/raw_address.h"

enum { HID_DEV_NO_CONN, HID_DEV_CONNECTED };
+24 −23
Original line number Diff line number Diff line
@@ -21,41 +21,42 @@
#include "stack/hid/hidh_int.h"
#include "stack/include/hci_error_code.h"
#include "test/common/mock_functions.h"
#include "test/mock/mock_stack_l2cap_api.h"
#include "test/mock/mock_stack_l2cap_interface.h"

bluetooth::common::MessageLoopThread* get_main_thread() { return nullptr; }
tHCI_REASON btm_get_acl_disc_reason_code(void) { return HCI_SUCCESS; }

namespace {
using ::testing::_;
using ::testing::DoAll;
using ::testing::NotNull;
using ::testing::Pointee;
using ::testing::Return;
using ::testing::ReturnArg;
using ::testing::SaveArg;
using ::testing::SaveArgPointee;
using ::testing::StrEq;
using ::testing::StrictMock;
using ::testing::Test;

using testing::_;
using testing::DoAll;
using testing::NotNull;
using testing::Pointee;
using testing::Return;
using testing::SaveArg;
using testing::SaveArgPointee;
using testing::StrEq;
using testing::StrictMock;
using testing::Test;
namespace {

class StackHidTest : public Test {
public:
protected:
  void SetUp() override { reset_mock_function_count_map(); }
  void TearDown() override {}
  void SetUp() override {
    reset_mock_function_count_map();
    bluetooth::testing::stack::l2cap::set_interface(&l2cap_interface_);
  }
  void TearDown() override { bluetooth::testing::stack::l2cap::reset_interface(); }

  bluetooth::testing::stack::l2cap::Mock l2cap_interface_;
  const tL2CAP_APPL_INFO* p_cb_info_;
};

TEST_F(StackHidTest, disconnect_bad_cid) {
  tL2CAP_APPL_INFO l2cap_callbacks;

  test::mock::stack_l2cap_api::L2CA_RegisterWithSecurity.body =
          [&l2cap_callbacks](uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info, bool enable_snoop,
                             tL2CAP_ERTM_INFO* p_ertm_info, uint16_t my_mtu,
                             uint16_t required_remote_mtu, uint16_t sec_level) {
            l2cap_callbacks = p_cb_info;
            return psm;
          };
  tL2CAP_APPL_INFO l2cap_callbacks{};
  EXPECT_CALL(l2cap_interface_, L2CA_RegisterWithSecurity(_, _, _, _, _, _, _))
          .WillRepeatedly(DoAll(SaveArg<1>(&l2cap_callbacks), ::testing::ReturnArg<0>()));

  tHID_STATUS status = hidh_conn_reg();
  ASSERT_EQ(HID_SUCCESS, status);