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

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

Merge "Add missing flag of socket_settings_api" into main

parents 11e3087a 90056860
Loading
Loading
Loading
Loading
+112 −25
Original line number Diff line number Diff line
@@ -561,18 +561,22 @@ static void on_srv_l2cap_psm_connect_l(tBTA_JV_L2CAP_OPEN* p_open, l2cap_socket*
  sock->handle = -1; /* We should no longer associate this handle with the server socket */
  accept_rs->is_le_coc = sock->is_le_coc;
  accept_rs->tx_mtu = sock->tx_mtu = p_open->tx_mtu;
  if (com::android::bluetooth::flags::socket_settings_api()) {  // Added with aosp/3349374
    accept_rs->rx_mtu = sock->rx_mtu;
  }
  accept_rs->local_cid = p_open->local_cid;
  accept_rs->remote_cid = p_open->remote_cid;
  // TODO(b/342012881) Remove connection uuid when offload socket API is landed.
  Uuid uuid = Uuid::From128BitBE(bluetooth::os::GenerateRandom<Uuid::kNumBytes128>());
  accept_rs->conn_uuid = uuid;
  if (com::android::bluetooth::flags::socket_settings_api()) {  // Added with aosp/3349374
    accept_rs->socket_id = btif_l2cap_sock_generate_socket_id();
    accept_rs->data_path = sock->data_path;
    strncpy(accept_rs->socket_name, sock->socket_name, sizeof(accept_rs->socket_name) - 1);
    accept_rs->socket_name[sizeof(accept_rs->socket_name) - 1] = '\0';
    accept_rs->hub_id = sock->hub_id;
    accept_rs->endpoint_id = sock->endpoint_id;
  }

  /* Swap IDs to hand over the GAP connection to the accepted socket, and start
     a new server on the newly create socket ID. */
@@ -601,8 +605,10 @@ static void on_srv_l2cap_psm_connect_l(tBTA_JV_L2CAP_OPEN* p_open, l2cap_socket*
  // one or the accept socket one.
  btsock_l2cap_server_listen(sock);
  // start monitoring the socketpair to get call back when app is accepting on server socket
  if (com::android::bluetooth::flags::socket_settings_api()) {  // Added with aosp/3349375
    btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD, sock->id);
  }
}

static void on_cl_l2cap_psm_connect_l(tBTA_JV_L2CAP_OPEN* p_open, l2cap_socket* sock) {
  sock->addr = p_open->rem_bda;
@@ -612,7 +618,9 @@ static void on_cl_l2cap_psm_connect_l(tBTA_JV_L2CAP_OPEN* p_open, l2cap_socket*
  // TODO(b/342012881) Remove connection uuid when offload socket API is landed.
  Uuid uuid = Uuid::From128BitBE(bluetooth::os::GenerateRandom<Uuid::kNumBytes128>());
  sock->conn_uuid = uuid;
  if (com::android::bluetooth::flags::socket_settings_api()) {  // Added with aosp/3349374
    sock->socket_id = btif_l2cap_sock_generate_socket_id();
  }

  if (!send_app_psm_or_chan_l(sock)) {
    log::error("Unable to send l2cap socket to application socket_id:{}", sock->id);
@@ -655,7 +663,8 @@ static void on_l2cap_connect(tBTA_JV* p_data, uint32_t id) {

  sock->tx_mtu = le_open->tx_mtu;
  if (psm_open->status == tBTA_JV_STATUS::SUCCESS) {
    if (sock->data_path == BTSOCK_DATA_PATH_NO_OFFLOAD) {
    if (!com::android::bluetooth::flags::socket_settings_api() ||  // Added with aosp/3349378
        sock->data_path == BTSOCK_DATA_PATH_NO_OFFLOAD) {
      if (!sock->server) {
        on_cl_l2cap_psm_connect_l(psm_open, sock);
      } else {
@@ -870,10 +879,12 @@ static void btsock_l2cap_server_listen(l2cap_socket* sock) {
  /* For hardware offload data path, host stack sets the initial credits to 0. The offload stack
   * should send initial credits to peer device through L2CAP signaling command when the data path
   * is switched successfully. */
  if (com::android::bluetooth::flags::socket_settings_api()) {  // Added with aosp/3349376
    if (sock->data_path == BTSOCK_DATA_PATH_HARDWARE_OFFLOAD) {
      cfg->init_credit_present = true;
      cfg->init_credit = 0;
    }
  }

  std::unique_ptr<tL2CAP_ERTM_INFO> ertm_info;
  if (!sock->is_le_coc) {
@@ -953,13 +964,15 @@ static bt_status_t btsock_l2cap_listen_or_connect(const char* name, const RawAdd
  sock->channel = channel;
  sock->app_uid = app_uid;
  sock->is_le_coc = is_le_coc;
  if (data_path == BTSOCK_DATA_PATH_HARDWARE_OFFLOAD) {
  if (com::android::bluetooth::flags::socket_settings_api() &&  // Added with aosp/3349377
      data_path == BTSOCK_DATA_PATH_HARDWARE_OFFLOAD) {
    if (!btsock_l2cap_get_offload_mtu(&sock->rx_mtu, static_cast<uint16_t>(max_rx_packet_size))) {
      return BT_STATUS_UNSUPPORTED;
    }
  } else {
    sock->rx_mtu = is_le_coc ? L2CAP_SDU_LENGTH_LE_MAX : L2CAP_SDU_LENGTH_MAX;
  }
  if (com::android::bluetooth::flags::socket_settings_api()) {  // Added with aosp/3349374
    sock->data_path = data_path;
    if (socket_name) {
      strncpy(sock->socket_name, socket_name, sizeof(sock->socket_name) - 1);
@@ -967,12 +980,15 @@ static bt_status_t btsock_l2cap_listen_or_connect(const char* name, const RawAdd
    }
    sock->hub_id = hub_id;
    sock->endpoint_id = endpoint_id;
  }

  /* "role" is never initialized in rfcomm code */
  if (listen) {
    btsock_l2cap_server_listen(sock);
    // start monitoring the socketpair to get call back when app is accepting on server socket
    if (com::android::bluetooth::flags::socket_settings_api()) {  // Added with aosp/3349375
      btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD, sock->id);
    }
  } else {
    tBTA_JV_CONN_TYPE connection_type =
            sock->is_le_coc ? tBTA_JV_CONN_TYPE::L2CAP_LE : tBTA_JV_CONN_TYPE::L2CAP;
@@ -983,10 +999,12 @@ static bt_status_t btsock_l2cap_listen_or_connect(const char* name, const RawAdd
    /* For hardware offload data path, host stack sets the initial credits to 0. The offload stack
     * should send initial credits to peer device through L2CAP signaling command when the data path
     * is switched successfully. */
    if (com::android::bluetooth::flags::socket_settings_api()) {  // Added with aosp/3349376
      if (sock->data_path == BTSOCK_DATA_PATH_HARDWARE_OFFLOAD) {
        cfg->init_credit_present = true;
        cfg->init_credit = 0;
      }
    }

    std::unique_ptr<tL2CAP_ERTM_INFO> ertm_info;
    if (!sock->is_le_coc) {
@@ -1130,7 +1148,7 @@ bool btsock_l2cap_read_signaled_on_listen_socket(int fd, int /* flags */, uint32
  return true;
}

void btsock_l2cap_signaled(int fd, int flags, uint32_t user_id) {
void btsock_l2cap_signaled_flagged(int fd, int flags, uint32_t user_id) {
  char drop_it = false;

  /* We use MSG_DONTWAIT when sending data to JAVA, hence it can be accepted to
@@ -1167,6 +1185,75 @@ void btsock_l2cap_signaled(int fd, int flags, uint32_t user_id) {
  }
}

void btsock_l2cap_signaled(int fd, int flags, uint32_t user_id) {
  if (com::android::bluetooth::flags::socket_settings_api()) {  // Added with aosp/3349375
    btsock_l2cap_signaled_flagged(fd, flags, user_id);
    return;
  }
  char drop_it = false;

  /* We use MSG_DONTWAIT when sending data to JAVA, hence it can be accepted to
   * hold the lock. */
  std::unique_lock<std::mutex> lock(state_lock);
  l2cap_socket* sock = btsock_l2cap_find_by_id_l(user_id);
  if (!sock) {
    return;
  }

  if ((flags & SOCK_THREAD_FD_RD) && !sock->server) {
    // app sending data
    if (sock->connected) {
      int size = 0;
      bool ioctl_success = ioctl(sock->our_fd, FIONREAD, &size) == 0;
      if (!(flags & SOCK_THREAD_FD_EXCEPTION) || (ioctl_success && size)) {
        /* FIONREAD return number of bytes that are immediately available for
           reading, might be bigger than awaiting packet.

           BluetoothSocket.write(...) guarantees that any packet send to this
           socket is broken into pieces no bigger than MTU bytes (as requested
           by BT spec). */
        size = std::min(size, (int)sock->tx_mtu);

        BT_HDR* buffer = malloc_l2cap_buf(size);
        /* The socket is created with SOCK_SEQPACKET, hence we read one message
         * at the time. */
        ssize_t count;
        OSI_NO_INTR(count = recv(fd, get_l2cap_sdu_start_ptr(buffer), size,
                                 MSG_NOSIGNAL | MSG_DONTWAIT | MSG_TRUNC));
        if (count > sock->tx_mtu) {
          /* This can't happen thanks to check in BluetoothSocket.java but leave
           * this in case this socket is ever used anywhere else*/
          log::error("recv more than MTU. Data will be lost: {}", count);
          count = sock->tx_mtu;
        }

        /* When multiple packets smaller than MTU are flushed to the socket, the
           size of the single packet read could be smaller than the ioctl
           reported total size of awaiting packets. Hence, we adjust the buffer
           length. */
        buffer->len = count;

        // will take care of freeing buffer
        BTA_JvL2capWrite(sock->handle, PTR_TO_UINT(buffer), buffer, user_id);
      }
    } else {
      drop_it = true;
    }
  }
  if (flags & SOCK_THREAD_FD_WR) {
    // app is ready to receive more data, tell stack to enable the data flow
    if (flush_incoming_que_on_wr_signal_l(sock) && sock->connected) {
      btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_WR, sock->id);
    }
  }
  if (drop_it || (flags & SOCK_THREAD_FD_EXCEPTION)) {
    int size = 0;
    if (drop_it || ioctl(sock->our_fd, FIONREAD, &size) != 0 || size == 0) {
      btsock_l2cap_free_l(sock);
    }
  }
}

bt_status_t btsock_l2cap_disconnect(const RawAddress* bd_addr) {
  if (!bd_addr) {
    return BT_STATUS_PARM_INVALID;
+4 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
#include "main/shim/stack.h"

#include <bluetooth/log.h>
#include <com_android_bluetooth_flags.h>
#include <fcntl.h>
#include <unistd.h>

@@ -76,7 +77,9 @@ void Stack::StartEverything() {
#if TARGET_FLOSS
  modules.add<sysprops::SyspropsModule>();
#else
  if (com::android::bluetooth::flags::socket_settings_api()) {  // Added with aosp/3286716
    modules.add<lpp::LppOffloadManager>();
  }
#endif
  modules.add<metrics::CounterMetrics>();
  modules.add<hal::HciHal>();
+10 −4
Original line number Diff line number Diff line
@@ -213,8 +213,12 @@ uint16_t GAP_ConnOpen(const char* /* p_serv_name */, uint8_t service_id, bool is

  /* Configure L2CAP COC, if transport is LE */
  if (transport == BT_TRANSPORT_LE) {
    if (com::android::bluetooth::flags::socket_settings_api()) {  // Added with aosp/3349376
      p_ccb->local_coc_cfg.credits =
              (p_ccb->cfg.init_credit_present) ? p_ccb->cfg.init_credit : L2CA_LeCreditDefault();
    } else {
      p_ccb->local_coc_cfg.credits = L2CA_LeCreditDefault();
    }
    p_ccb->local_coc_cfg.mtu = p_cfg->mtu;

    uint16_t max_mps = bluetooth::shim::GetController()->GetLeBufferSize().le_data_packet_length_;
@@ -752,8 +756,10 @@ static void gap_checks_con_flags(tGAP_CCB* p_ccb) {
      cb_data.l2cap_cids.remote_cid = l2cap_remote_cid;
      cb_data_ptr = &cb_data;
    }
    if (com::android::bluetooth::flags::socket_settings_api()) {  // Added with aosp/3367197
      stack::l2cap::get_interface().L2CA_GetRemoteChannelId(p_ccb->local_cid, &p_ccb->remote_cid);
      stack::l2cap::get_interface().L2CA_GetAclHandle(p_ccb->local_cid, &p_ccb->acl_handle);
    }
    p_ccb->con_state = GAP_CCB_STATE_CONNECTED;

    p_ccb->p_callback(p_ccb->gap_handle, GAP_EVT_CONN_OPENED, cb_data_ptr);
+15 −5
Original line number Diff line number Diff line
@@ -777,11 +777,21 @@ void l2cble_process_sig_cmd(tL2C_LCB* p_lcb, uint8_t* p, uint16_t pkt_len) {
      p_ccb->p_rcb = p_rcb;
      p_ccb->remote_cid = rcid;

      if (com::android::bluetooth::flags::socket_settings_api()) {  // Added with aosp/3349377
        p_ccb->local_conn_cfg.mtu = p_rcb->coc_cfg.mtu;
        p_ccb->local_conn_cfg.mps = p_rcb->coc_cfg.mps;
      } else {
        p_ccb->local_conn_cfg.mtu = L2CAP_SDU_LENGTH_LE_MAX;
        p_ccb->local_conn_cfg.mps =
                bluetooth::shim::GetController()->GetLeBufferSize().le_data_packet_length_;
      }
      if (com::android::bluetooth::flags::socket_settings_api()) {  // Added with aosp/3349376
        p_ccb->local_conn_cfg.credits = p_rcb->coc_cfg.credits;

        p_ccb->remote_credit_count = p_rcb->coc_cfg.credits;
      } else {
        p_ccb->local_conn_cfg.credits = L2CA_LeCreditDefault();
        p_ccb->remote_credit_count = L2CA_LeCreditDefault();
      }

      p_ccb->peer_conn_cfg.mtu = mtu;
      p_ccb->peer_conn_cfg.mps = mps;