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

Commit add32ae3 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge changes I5fad5161,I7ce390ca,Ib0b4ea11,I6cc72e13,Ic4a66b2c, ...

* changes:
  Re-log bta/gatt::bta_gattc_conn_cback
  Re-include btif/src/btif_sock_l2cap
  Eliminate socket verbosity print_events()
  Re-log stack/acl::BTM_SwitchRole
  Reduce identical APIs
  Remove #def code conditional BTM_MAX_SCO_LINKS
  Remove unused #defs stack/btm/btm_ble_int_types::
  Internally link stack/btm/btm_ble_gap::btm_send_hci_scan_enable
  Use new APIS stack/acl/btm_acl
  Streamline stack/acl/btm_acl::btm_acl_created
  Add new APIS stack/acl/btm_acl
parents 7296c091 2a365118
Loading
Loading
Loading
Loading
+9 −8
Original line number Diff line number Diff line
@@ -1024,16 +1024,17 @@ static void bta_gattc_conn_cback(tGATT_IF gattc_if, const RawAddress& bdaddr,
                                 uint16_t conn_id, bool connected,
                                 tGATT_DISCONN_REASON reason,
                                 tBT_TRANSPORT transport) {
  if (reason != 0) {
    LOG(WARNING) << __func__ << ": cif=" << +gattc_if
                 << " connected=" << connected << " conn_id=" << loghex(conn_id)
                 << " reason=" << loghex(reason);
  }

  if (connected)
  if (connected) {
    LOG_INFO("Connected transport:%s reason:%s",
             BtTransportText(transport).c_str(),
             hci_error_code_text(reason).c_str());
    btif_debug_conn_state(bdaddr, BTIF_DEBUG_CONNECTED, GATT_CONN_UNKNOWN);
  else
  } else {
    LOG_INFO("Disconnected transport:%s reason:%s",
             BtTransportText(transport).c_str(),
             hci_error_code_text(reason).c_str());
    btif_debug_conn_state(bdaddr, BTIF_DEBUG_DISCONNECTED, reason);
  }

  tBTA_GATTC_DATA* p_buf =
      (tBTA_GATTC_DATA*)osi_calloc(sizeof(tBTA_GATTC_DATA));
+12 −22
Original line number Diff line number Diff line
@@ -15,34 +15,24 @@
 * limitations under the License.
 */

#include "btif_sock_l2cap.h"

#include <base/logging.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <vector>

#include <mutex>

#include <frameworks/base/core/proto/android/bluetooth/enums.pb.h>
#include <hardware/bt_sock.h>

#include "osi/include/allocator.h"
#include <cstdint>
#include <cstring>

#include "bt_target.h"
#include "bta_jv_api.h"
#include "btif_sock_thread.h"
#include "btif_sock_util.h"
#include "btif_uid.h"
#include "btif_util.h"
#include "btm_api.h"
#include "bta/include/bta_jv_api.h"
#include "btif/include/btif_sock_thread.h"
#include "btif/include/btif_sock_util.h"
#include "btif/include/btif_uid.h"
#include "common/metrics.h"
#include "l2c_api.h"
#include "l2cdefs.h"
#include "include/hardware/bluetooth.h"
#include "internal_include/bt_target.h"
#include "osi/include/log.h"
#include "osi/include/osi.h"
#include "stack/btm/security_device_record.h"
#include "stack/include/bt_types.h"
#include "types/raw_address.h"

struct packet {
  struct packet *next, *prev;
+0 −13
Original line number Diff line number Diff line
@@ -443,18 +443,6 @@ static int process_cmd_sock(int h) {
  return true;
}

static void print_events(short events) {
  std::string flags("");
  if ((events)&POLLIN) flags += " POLLIN";
  if ((events)&POLLPRI) flags += " POLLPRI";
  if ((events)&POLLOUT) flags += " POLLOUT";
  if ((events)&POLLERR) flags += " POLLERR";
  if ((events)&POLLHUP) flags += " POLLHUP ";
  if ((events)&POLLNVAL) flags += " POLLNVAL";
  if ((events)&POLLRDHUP) flags += " POLLRDHUP";
  LOG_DEBUG("print poll event:%x = %s", (events), flags.c_str());
}

static void process_data_sock(int h, struct pollfd* pfds, int count) {
  asrt(count <= ts[h].poll_count);
  int i;
@@ -465,7 +453,6 @@ static void process_data_sock(int h, struct pollfd* pfds, int count) {
      uint32_t user_id = ts[h].ps[ps_i].user_id;
      int type = ts[h].ps[ps_i].type;
      int flags = 0;
      print_events(pfds[i].revents);
      if (IS_READ(pfds[i].revents)) {
        flags |= SOCK_THREAD_FD_RD;
      }
+1 −0
Original line number Diff line number Diff line
@@ -192,6 +192,7 @@ typedef struct {
  friend void BTM_acl_after_controller_started();
  friend void BTM_default_block_role_switch();
  friend void BTM_default_unblock_role_switch();
  friend void acl_initialize_power_mode(const tACL_CONN& p_acl);
  friend void acl_set_disconnect_reason(uint8_t acl_disc_reason);
  friend void btm_acl_created(const RawAddress& bda, uint16_t hci_handle,
                              uint8_t link_role, tBT_TRANSPORT transport);
+112 −90
Original line number Diff line number Diff line
@@ -60,6 +60,7 @@
#include "types/raw_address.h"

struct StackAclBtmAcl {
  tACL_CONN* acl_allocate_connection();
  tACL_CONN* acl_get_connection_from_handle(uint16_t handle);
  tACL_CONN* btm_bda_to_acl(const RawAddress& bda, tBT_TRANSPORT transport);
  tBTM_STATUS btm_set_packet_types(tACL_CONN* p, uint16_t pkt_types);
@@ -331,10 +332,25 @@ void btm_acl_process_sca_cmpl_pkt(uint8_t len, uint8_t* data) {
 * Returns          void
 *
 ******************************************************************************/
void acl_initialize_power_mode(const tACL_CONN& p_acl) {
  tBTM_PM_MCB* p_db =
      &btm_cb.acl_cb_.pm_mode_db[btm_handle_to_acl_index(p_acl.hci_handle)];
  memset(p_db, 0, sizeof(tBTM_PM_MCB));
  p_db->state = BTM_PM_ST_ACTIVE;
}

tACL_CONN* StackAclBtmAcl::acl_allocate_connection() {
  tACL_CONN* p_acl = &btm_cb.acl_cb_.acl_db[0];
  for (uint8_t xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_acl++) {
    if (!p_acl->in_use) {
      return p_acl;
    }
  }
  return nullptr;
}

void btm_acl_created(const RawAddress& bda, uint16_t hci_handle,
                     uint8_t link_role, tBT_TRANSPORT transport) {
  tBTM_SEC_DEV_REC* p_dev_rec = NULL;
  uint8_t xx;

  tACL_CONN* p_acl = internal_.btm_bda_to_acl(bda, transport);
  if (p_acl != (tACL_CONN*)NULL) {
@@ -350,13 +366,15 @@ void btm_acl_created(const RawAddress& bda, uint16_t hci_handle,
    return;
  }

  LOG_DEBUG("Acl created handle:%hu role:%s transport:%s", hci_handle,
  p_acl = internal_.acl_allocate_connection();
  if (p_acl == nullptr) {
    LOG_ERROR("Unable to find available acl resource to continue");
    return;
  }

  LOG_DEBUG("Acl allocated handle:%hu role:%s transport:%s", hci_handle,
            RoleText(link_role).c_str(), BtTransportText(transport).c_str());

  /* Allocate acl_db entry */
  for (xx = 0, p_acl = &btm_cb.acl_cb_.acl_db[0]; xx < MAX_L2CAP_LINKS;
       xx++, p_acl++) {
    if (!p_acl->in_use) {
  p_acl->in_use = true;
  p_acl->hci_handle = hci_handle;
  p_acl->link_role = link_role;
@@ -366,30 +384,37 @@ void btm_acl_created(const RawAddress& bda, uint16_t hci_handle,
  btm_set_link_policy(p_acl, btm_cb.acl_cb_.btm_def_link_policy);

  p_acl->transport = transport;
      if (transport == BT_TRANSPORT_LE)
  if (transport == BT_TRANSPORT_LE) {
    btm_ble_refresh_local_resolvable_private_addr(
        bda, btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr);
  }
  p_acl->switch_role_failed_attempts = 0;
  p_acl->reset_switch_role();

      tBTM_PM_MCB* p_db = &btm_cb.acl_cb_.pm_mode_db[xx]; /* per ACL link */
      memset(p_db, 0, sizeof(tBTM_PM_MCB));
      p_db->state = BTM_PM_ST_ACTIVE;
  acl_initialize_power_mode(*p_acl);

  /* if BR/EDR do something more */
  if (transport == BT_TRANSPORT_BR_EDR) {
        btsnd_hcic_read_rmt_clk_offset(p_acl->hci_handle);
        btsnd_hcic_rmt_ver_req(p_acl->hci_handle);
    btsnd_hcic_read_rmt_clk_offset(hci_handle);
    btsnd_hcic_rmt_ver_req(hci_handle);
  }

  tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev_by_handle(hci_handle);
  if (p_dev_rec == nullptr) {
    LOG_ERROR("Unable to find security device record hci_handle:%hu",
              hci_handle);
    // TODO Release acl resource
    return;
  }
      p_dev_rec = btm_find_dev_by_handle(hci_handle);

      if (p_dev_rec && !(transport == BT_TRANSPORT_LE)) {
  if (transport != BT_TRANSPORT_LE) {
    /* If remote features already known, copy them and continue connection
     * setup */
    if ((p_dev_rec->num_read_pages) &&
        (p_dev_rec->num_read_pages <= (HCI_EXT_FEATURES_PAGE_MAX + 1))) {
      memcpy(p_acl->peer_lmp_feature_pages, p_dev_rec->feature_pages,
             (HCI_FEATURE_BYTES_PER_PAGE * p_dev_rec->num_read_pages));
      // TODO We do not need to store the pages read here
      p_acl->num_read_pages = p_dev_rec->num_read_pages;

      const uint8_t req_pend = (p_dev_rec->sm4 & BTM_SM4_REQ_PEND);
@@ -406,8 +431,7 @@ void btm_acl_created(const RawAddress& bda, uint16_t hci_handle,
    }
  }

      /* If here, features are not known yet */
      if (p_dev_rec && transport == BT_TRANSPORT_LE) {
  if (transport == BT_TRANSPORT_LE) {
    btm_ble_get_acl_remote_addr(p_dev_rec, p_acl->active_remote_addr,
                                &p_acl->active_remote_addr_type);

@@ -419,11 +443,6 @@ void btm_acl_created(const RawAddress& bda, uint16_t hci_handle,
      internal_.btm_establish_continue(p_acl);
    }
  }

      /* read page 1 - on rmt feature event for buffer reasons */
      return;
    }
  }
}

void btm_acl_update_conn_addr(uint16_t conn_handle, const RawAddress& address) {
@@ -543,53 +562,56 @@ tBTM_STATUS BTM_GetRole(const RawAddress& remote_bd_addr, uint8_t* p_role) {
 *
 ******************************************************************************/
tBTM_STATUS BTM_SwitchRole(const RawAddress& remote_bd_addr, uint8_t new_role) {

  LOG_INFO("%s: peer %s new_role=0x%x", __func__,
           remote_bd_addr.ToString().c_str(), new_role);

  /* Make sure the local device supports switching */
  if (!controller_get_interface()->supports_master_slave_role_switch())
    return (BTM_MODE_UNSUPPORTED);
  if (!controller_get_interface()->supports_master_slave_role_switch()) {
    LOG_INFO("Local controller does not support role switching");
    return BTM_MODE_UNSUPPORTED;
  }

  tACL_CONN* p_acl =
      internal_.btm_bda_to_acl(remote_bd_addr, BT_TRANSPORT_BR_EDR);
  if (p_acl == NULL) return (BTM_UNKNOWN_ADDR);
  if (p_acl == nullptr) {
    LOG_INFO("Unable to find device for classic role switch");
    return BTM_UNKNOWN_ADDR;
  }

  /* Finished if already in desired role */
  if (p_acl->link_role == new_role) return (BTM_SUCCESS);
  if (p_acl->link_role == new_role) {
    LOG_INFO("Requested role is already in effect");
    return BTM_SUCCESS;
  }

  if (interop_match_addr(INTEROP_DISABLE_ROLE_SWITCH, &remote_bd_addr))
  if (interop_match_addr(INTEROP_DISABLE_ROLE_SWITCH, &remote_bd_addr)) {
    LOG_INFO("Remote device is on list preventing role switch");
    return BTM_DEV_BLACKLISTED;
  }

  /* Check if there is any SCO Active on this BD Address */
  if (BTM_IsScoActiveByBdaddr(remote_bd_addr)) return (BTM_NO_RESOURCES);
  if (BTM_IsScoActiveByBdaddr(remote_bd_addr)) {
    LOG_INFO("An active SCO to device prevents role switch at this time");
    return BTM_NO_RESOURCES;
  }

  /* Ignore role switch request if the previous request was not completed */
  if (!p_acl->is_switch_role_idle()) {
    LOG_DEBUG("%s switch role in progress", __func__);
    LOG_DEBUG("Role switch is already progress");
    return BTM_BUSY;
  }

  if (interop_match_addr(INTEROP_DYNAMIC_ROLE_SWITCH, &remote_bd_addr)) {
    BTM_TRACE_DEBUG("%s, Device blacklisted under INTEROP_DYNAMIC_ROLE_SWITCH.",
                    __func__);
    return BTM_DEV_BLACKLISTED;
  }

  tBTM_PM_MODE pwr_mode;
  if (!BTM_ReadPowerMode(p_acl->remote_addr, &pwr_mode)) {
    LOG_WARN(
        "Unable to find device to read current power mode prior to role "
        "switch");
    return BTM_UNKNOWN_ADDR;
  };

  /* Wake up the link if in sniff or park before attempting switch */
  if (pwr_mode == BTM_PM_MD_PARK || pwr_mode == BTM_PM_MD_SNIFF) {
    tBTM_PM_PWR_MD settings;
    memset((void*)&settings, 0, sizeof(settings));
    settings.mode = BTM_PM_MD_ACTIVE;
    tBTM_STATUS status =
        BTM_SetPowerMode(BTM_PM_SET_ONLY_ID, p_acl->remote_addr, &settings);
    if (status != BTM_CMD_STARTED) return (BTM_WRONG_MODE);

    if (status != BTM_CMD_STARTED) {
      LOG_WARN("Unable to set power mode before attempting switch");
      return BTM_WRONG_MODE;
    }
    p_acl->set_switch_role_changing();
  }
  /* some devices do not support switch while encryption is on */
@@ -608,7 +630,7 @@ tBTM_STATUS BTM_SwitchRole(const RawAddress& remote_bd_addr, uint8_t new_role) {
    }
  }

  return (BTM_CMD_STARTED);
  return BTM_CMD_STARTED;
}

/*******************************************************************************
Loading